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 )