2

I want to make a Custom Application Launcher and then add it to my Panel so that every time I click on it, it copy's a pre-loaded line of text, but I don't know how to write the commands for it.

For example, if the pre-loaded line of text is "Hello". I would like it to be so that every time I click on it, it copies the word "Hello" to my clipboard so I can paste it wherever.

biljkus
  • 27
omurad
  • 23
  • Sounds doable, but how would you (like to) set the text to be pasted? Also, the title says: ...that copys a text to my clipboard? but it actually seems you want to paste it? – Jacob Vlijm Jul 08 '15 at 20:49
  • Furthermore, is this: http://askubuntu.com/a/544388/72216 something you have in mind, or is that to "large". – Jacob Vlijm Jul 08 '15 at 20:52
  • @JacobVlijm I do indeed need a text to be copied but if you're talking in perspective of the clipboard then yes, it is being pasted into the clipboard. To put it in pseudo code perspective: Application is clicked >> system copies the string "Hello" to clipboard. – omurad Jul 08 '15 at 20:59
  • ...But is "Hello" the only string you' d need, or should you have to make it "editable? – Jacob Vlijm Jul 08 '15 at 21:00
  • @JacobVlijm It should be editable but I have enough coding experience to decipher where in the code I need to edit in order to change the pre-loaded string. – omurad Jul 08 '15 at 21:02
  • That seems not the most user-friendly option :) – Jacob Vlijm Jul 08 '15 at 21:02
  • @JacobVlijm Very nice of you to put thought into it. Is there a specific programming language that the commands for the application launcher takes? – omurad Jul 08 '15 at 21:06
  • With a launcher, you can run practically anything/language. The launcher is the easy part. Would you like an example? (python) – Jacob Vlijm Jul 08 '15 at 21:07
  • Whay you've described can be simply done with a simple one line script 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
  • Of course! @Serg – omurad Jul 13 '15 at 12:17

1 Answers1

4

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:

  • click on the launcher to copy a pre- defined string or line to the clipboard, or:
  • right-click on it to set a new string:

    enter image description here
    enter image description here

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:

  1. 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(!!)

  2. 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:

enter image description here

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:

enter image description here

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.

enter image description here

Jacob Vlijm
  • 83,767