Editing gsettings; add icon to launcher by command [duplicate]

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


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']/")"

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 launcher_append_item.py sakura.desktop 3  

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

python launcher_append_item.py 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 = Gio.Settings.new(schema)
    else:
        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 = Gio.Settings.new(schema)
    else:
        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])
else:
   current_list.append(  'application://' + sys.argv[1]  )

gsettings_set( 'com.canonical.Unity.Launcher', None, 'favorites',current_list  )