如何通过命令行向Unity启动器添加/删除应用程序?

我正在使用UCK(Ubuntu定制工具包)自定义一个Ubuntu 14.04的Live CD。该程序在终端中提供了一个chroot环境,用于进行更改。
我想要添加和删除出现在dock上的程序。
我不确定是否可以通过修改.desktop文件来实现这一目标?
如何使用终端完成这个任务?

答案在这个页面上:但是我现在没有时间。很快会给你一个正式的回答。这个链接也可能有所帮助。 - Tim
Aaron,抱歉,你得原谅我。我只读了你问题的一半。我编辑了这个链接上的所谓重复内容:http://askubuntu.com/questions/522949/adding-my-launcher-to-the-unity-launcher-automatically-how,以便通过命令行添加*和*从启动器中删除项目。 - Jacob Vlijm
1个回答

以下脚本可根据参数的不同,用于向启动器添加或移除项目。
#!/usr/bin/env python3

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 set_launcher(desktopfile):
    curr_launcher = current_launcher()
    last = [i for i, x in enumerate(curr_launcher) if x.startswith("application://")][-1]
    new_icon = "application://"+desktopfile
    if sys.argv[2] == "a":
        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)])
    elif sys.argv[2] == "r":
        curr_launcher.remove(new_icon)
        subprocess.Popen(["gsettings", "set", "com.canonical.Unity.Launcher","favorites",str(curr_launcher)])

set_launcher(desktopfile)

如何运行它

  1. 将代码粘贴到一个空文件中,保存为set_launcher.py
  2. 通过以下命令运行它:

    python3 /path/to/set_launcher.py <name_of_.desktop_file> a
    

    添加图标,或者:

    python3 /path/to/set_launcher.py <name_of_.desktop_file> r
    

    移除图标

    示例:

    python3 /path/to/set_launcher.py gedit.desktop a
    

    gedit添加到启动器,或者

    python3 /path/to/set_launcher.py gedit.desktop r
    

    从启动器中移除gedit

解释

启动器图标列表在键中定义:

com.canonical.Unity.Launcher favorites

可以通过以下命令获取:
gsettings get com.canonical.Unity.Launcher favorites

为了设置一个备选列表(考虑到您使用正确的格式):
gsettings set com.canonical.Unity.Launcher favorites "[item1, item2, etc]"

你可以通过编辑一个 .desktop 文件来实现这个吗?
不,与文件本身无关。重要的是该文件是否在启动器收藏夹列表中。
从命令行编辑此列表正是脚本所做的事情。

"set_launcher(desktopfile)" 这个在代码下方显示的部分,实际上是代码的一部分吗? - Aaron Salyer
@AaronSalyer 真是太粗心了!我忘记缩进了。已经修复好了 :) - Jacob Vlijm
1感谢不仅提供代码,还提供了分解说明。这使得理解脚本的功能并以其他方式复制它变得更容易。这在UCK环境和我的14.04安装中运行得非常好! - Aaron Salyer
在18.04版本中,我无法通过dconf-editor或类似工具找到所提及的模式。请问现在它存储在哪里?谢谢! - artu-hnrq