编辑gsettings; 通过命令将图标添加到启动器

虽然互联网上有几篇关于这个主题的post,但我还没有找到解决方案:
我的目标是找到一个命令,将一个图标(.desktop文件)添加到Unity启动器并立即显示它。 当我打开dconf-editor (桌面>统一>启动器)并将项目添加到collections夹列表时,它会立即显示在启动器中,所以我的想法是必须可以通过命令执行相同的操作。 我到目前为止在互联网上找到的解决方案并不能胜任。

我需要通过命令来完成它,以便在我正在使用的快速列表编辑器中使用。

如果你能提供帮助,你会让某人难以置信地高兴

您也可以使用gsettings工具对dconf进行操作。

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

接受的答案是正确的,但由于使用sed和许多逃逸序列而很麻烦。 波纹管pythonic解决方案更清晰,并且允许简单地指定要附加的.desktop文件,并且可选地,您可以在启动器上指定位置。

例如,

 python launcher_append_item.py sakura.desktop 3 

sakura作为第4个图标(因为列表索引从0开始)。 简单地跑

 python launcher_append_item.py sakura.desktop 

会将图标附加到列表中。

为了进一步考虑,人们甚至可以添加一个选项来替换启动器上的特定项目与其他项目。 但这是未来思考的练习:)

源代码

 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 )