3

Based on official documentation:

In order to add your launcher to the Unity Launcher on the left, you select and drag it onto the Launcher panel. Alternatively, you can place your .desktop file at /usr/share/applications/ or at ~/.local/share/applications/. After moving your file there, search for it in the Dash (Windows key -> type the name of the application) and drag and drop it to the Unity Launcher. Now your launcher (.desktop file) is locked on the Unity Launcher!

It means, that you can add a launcher manually via "drag and drop". But, is it possible to do this programatically?

I can explain why I am asking about it:

I have my own scpirt, it downloads the latest version of Eclipse IDE, creates eclipse.desktop file without iteraction with User.

And on the last step I want to add Eclipse.desktop file to Unity Launcher and I do not want to ask user to do it.

I analized the spesification for *.desktop files and did get an answer.

Please provide advice.

1
  • Can you please provide (a small snippet) of your script where you create the .desktop file? We might be able to help you get to a better solution this way. :)
    – Gladen
    Sep 11, 2014 at 20:56

1 Answer 1

5

I actually made a script for that. It places the icon down in the launcher, but with a minor change, you can put it at the top (or any other position) as well.

As it is, it is in python2, but simply change the shebang to #!/usr/bin/env python3 if you'd like to use it as python3. The code is identical.

To use it, the .desktop file needs to be in either /usr/share/applications or in ~/.local/share/applications, but usually that is the case.

How to use

  • copy the script below, save it as launcher_add.py
  • make it executable

Run it by the command:

/path/to/launcher_add.py name_of_desktopfile.desktop 

you have to use the filename of the .desktop file, without the path.

The script

#!/usr/bin/env python

import subprocess
import sys

desktopfile = sys.argv[1]

def current_launcher():
    get_current = subprocess.check_output(["gsettings", "get", "com.canonical.Unity.Launcher", "favorites"]).decode("utf-8")
    return eval(get_current)

def add_new(desktopfile):
    curr_launcher = current_launcher()
    last = [i for i, x in enumerate(curr_launcher) if x.startswith("application://")][-1]
    new_icon = "application://"+desktopfile
    if not new_icon in curr_launcher:
        curr_launcher.insert(last, new_icon)
        subprocess.Popen(["gsettings", "set", "com.canonical.Unity.Launcher","favorites",str(curr_launcher)])
    else:
        pass

add_new(desktopfile)

The script prevents multiple occasions in the launcher list of the same application, which would cause corruption of he list.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .