Below an example of an easily customizable setup. It exists of a script, and a .desktop
file (launcher) to run the script with two options. You can:
How to set up
To be able to copy text to the clipboard, we need to install xclip
:
sudo apt-get install xclip
Then we need two things:
The script:
#!/usr/bin/env python3
import subprocess
import os
import sys
wordfile = os.environ["HOME"]+"/.word_file"
def get_word():
try:
copy = "xclip -in -selection c "+"'"+wordfile+"'"
subprocess.Popen(["/bin/bash", "-c", copy])
except FileNotFoundError:
pass
def set_word():
try:
word = "zenity --entry --title='Change the word' --text='Set a new string or line'"
new_word = subprocess.check_output(["/bin/bash", "-c", word]).decode("utf-8").strip()
open(wordfile, "wt").write(new_word)
except subprocess.CalledProcessError:
pass
arg = sys.argv[1]
if arg == "-set":
set_word()
elif arg == "-get":
get_word()
Copy it into an empty file, save it as copy_text.py
and make it executable(!!)
A .desktop file
, to run the script with two options
[Desktop Entry]
Name=Paste a string
Exec=/path/to/copy_text.py -get
Type=Application
Icon=/path/to/your/icon.png
Actions=Set a new string;
[Desktop Action Set a new string]
Name=Set a new string
Exec=/path/to/copy_text.py -set
OnlyShowIn=Unity;
Copy it into an empty file, save it as paste_text.desktop
in ~/.local/share/applications
, replace in both the lines, starting with Exec=
:
/path/to/copy_text.py
by the actual path to the script, and replace in the line, starting with Icon=
/path/to/your/icon.png
by the actual path to a valid icon (e.g. a .png
icon of 64x64 px)
Now drag the .desktop
file to the Unity
launcher and you'll have your easily customizable setup.
Explanation
Using xclip in a .desktop file
xclip
is a utility that can be used to manage the clipboard from the command line.
To copy a string to the clipboard, you can use the command:
echo <string> | xclip -sel clip
To copy the content of a file to the clipboard:
xclip -in -selection c <file>
If it would be a fixed string that you need, you could indeed, as mentioned by Serg, do with a .desktop
file with a one- liner:
[Desktop Entry]
Name=Paste a string
Exec=/bin/bash -c "echo Hello World | xclip -sel clip"
Type=Application
Icon=/path/to/icon.png
or, if you would only have a limited number of (fixed) strings to use:
[Desktop Entry]
Name=Paste a string
Exec=/bin/bash -c "echo Monkey | xclip -sel clip"
Type=Application
Icon=/opt/lswitcher/icon/lsw_64.png
Actions=Monkey;eats;banana;
[Desktop Action Monkey]
Name=Copy Monkey
Exec=/bin/bash -c "echo Monkey | xclip -sel clip"
OnlyShowIn=Unity;
[Desktop Action eats]
Name=Copy eats
Exec=/bin/bash -c "echo eats | xclip -sel clip"
OnlyShowIn=Unity;
[Desktop Action banana]
Name=Copy banana
Exec=/bin/bash -c "echo banana | xclip -sel clip"
OnlyShowIn=Unity;
then create the file, as explained in the answer, and drag it over the Unity
launcher:

If the string to copy would be an unpredictable one, it would however mean that you would have to edit the .desktop
file manually every time you want to change the string, which is not very user- friendly.
What the script is for
Therfore, the script creates an external file, ~/.word_file
, with the content of the string you'd like to be copied to the clipboard. The script has two functions:
def get_word():
try:
copy = "xclip -in -selection c "+"'"+wordfile+"'"
subprocess.Popen(["/bin/bash", "-c", copy])
except FileNotFoundError:
pass
to (try to) read the text to be copied to the clipboard from the file, pass
if the file doesn't exist.
and:
def set_word():
try:
word = "zenity --entry --title='Change the word' --text='Set a new string or line'"
new_word = subprocess.check_output(["/bin/bash", "-c", word]).decode("utf-8").strip()
open(wordfile, "wt").write(new_word)
except subprocess.CalledProcessError:
pass
to open a zenity
window in which you can enter the new string or line to write to the file:

The .desktop
file
finally, calls the two functions of the script with two commands:
Exec=/path/to/copy_text.py -get
to call the script with the option -get
, as the "main" command in the .desktop
file, to be executed with a simple click on the Unity
launcher's icon, and:
Exec=/path/to/copy_text.py -set
to call the script with the option -set
, as a right-click (quicklist) option.

echo Hello | xclip -sel clip
. Xclip would need to be installed with apt-get though. Think I could post this as an answer ? – Sergiy Kolodyazhnyy Jul 08 '15 at 22:23