Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Although there are several posts on the Internet on the subject, I did not find a solution yet:
my goal is to find a command that adds an icon (.desktop file) to the Unity launcher and shows it immediately. when I open dconf-editor (desktop > unity > launcher) and I add an item to the favorites list, it shows at once in the launcher, so my idea is that it must be possible to do the same thing by command. The solutions I found so far on the Internet do not do the job.

I need to do it by command, to use in a quicklist editor I am working on.

You would make someone unbelievably happy if you could help out

share|improve this question

marked as duplicate by Jacob Vlijm, Community 2 days ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 12 down vote accepted

You can act on dconf also with gsettings tool.

gsettings set com.canonical.Unity.Launcher favorites "$(gsettings get com.canonical.Unity.Launcher favorites | sed "s/, *'yourapp' *//g" | sed "s/'yourapp' *, *//g" | sed -e "s/]$/, 'yourapp']/")"
share|improve this answer
perfect, absolutely perfect, thanks!! – Jacob Vlijm Jun 20 '12 at 14:27
@Jacob, @jasmines This does not work in a chroot environment. gsettings says twice ** (process:10439): WARNING **: The connection is closed, but does not change the value (get still returns the old one). Any hint about this? Isn't this value stored in a plain text file somewhere? – Didier Trosset Oct 4 '12 at 7:18
@Didier Trosset, You cannot run gsettings as root -- it must be run as your userid. – user220845 Nov 30 '13 at 19:15
@Pete Howell: Why not ? Works perfectly on my machine. – WitchCraft Dec 6 '13 at 6:27

The accepted answer is alright, but cumbersome due to use of sed and lots of escape sequences. The bellow pythonic solution is much cleaner, and allows simply specify what .desktop file you want appended, and optionally you can specify a position on the launcher.

For example,

python sakura.desktop 3  

would place sakura as 4th icon (because list indexes start with 0). Running simply

python sakura.desktop  

would append the icon to the list.

For further thought, one could even add an option for replacing a specific item on launcher with some other item. But that's an exercise for future contemplation :)

source code

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gio,Gtk
import dbus
import sys

def gsettings_get(schema,path,key):
    if path is None:
        gsettings =
        gsettings = Gio.Settings.new_with_path(schema,path)
    return gsettings.get_value(key)

def gsettings_set(schema,path,key,value):
    if path is None:
        gsettings =
        gsettings = Gio.Settings.new_with_path(schema,path)
    return gsettings.set_strv(key,value)

current_list = list(gsettings_get('com.canonical.Unity.Launcher',None,'favorites'))

if sys.argv[2]:
   current_list.insert(int(sys.argv[2]),'application://' + sys.argv[1])
   current_list.append(  'application://' + sys.argv[1]  )

gsettings_set( 'com.canonical.Unity.Launcher', None, 'favorites',current_list  )
share|improve this answer
Thanks a lot! I realize the question is a dupe of one I answered later. – Jacob Vlijm 2 days ago

Not the answer you're looking for? Browse other questions tagged or ask your own question.