Ubuntu 16.04:如何通过终端向 Unity Launcher 添加/删除固定应用程序?

mrj*_*per 2 unity launcher 16.04

所以我在网上搜索了我的主题,但没有找到任何答案。

是否可以?如果是的话,你能告诉我吗?谢谢 :)

Ser*_*nyy 6

内容:

  1. Launcher操作的一般理论
  2. 删除和附加到 Unity 启动器的可能方法
  3. launcherctl.py 实用程序

1.发射器操作的一般理论

Unity 启动器本质上是一个.desktop文件列表。它们本质上是允许启动应用程序以及执行自定义操作的快捷方式。通常,它们存储在 中/usr/share/applications,但也可以位于 中~/.local/share/applications以及系统上的任何其他位置。对于一般情况,我建议/usr/share/applications为所有用户或~/.local/share/applications每个用户存储此类文件。

Dconf设置数据库允许存储等应用的统一发射器的列表,并且可以查看和结构改变gsettings的效用。例如:

$ gsettings get  com.canonical.Unity.Launcher favorites
['application://wps-office-et.desktop', 'application://wps-office-wpp.desktop', 'application://wps-office-wps.desktop', 'unity://running-apps', 'unity://devices']
$ gsettings set  com.canonical.Unity.Launcher favorites  "['wechat.desktop']"                                                         
$ gsettings get  com.canonical.Unity.Launcher favorites                                                                               
['application://wechat.desktop', 'unity://running-apps', 'unity://devices']
Run Code Online (Sandbox Code Playgroud)

如您所见,所有.desktop文件都有application://前缀,但是在设置启动器列表时不需要。带unity://前缀的项目不可更改且无法删除。

gsettings get com.canonical.Unity.Launcher favoritesgsettings set com.canonical.Unity.Launcher favorites命令可用于在创建功能~/.bashrc,例如:

get_launcher()
{
    gsettings get  com.canonical.Unity.Launcher favorites
}

set_launcher()
{
    # call this as set_launcher "['file1.desktop','file2.desktop']"
    gsettings set  com.canonical.Unity.Launcher favorites "$1"
}
Run Code Online (Sandbox Code Playgroud)

例子:

$ set_launcher "['firefox.desktop','gnome-terminal.desktop']"                                                                         
$ get_launcher                                                                                                                        
['application://firefox.desktop', 'application://gnome-terminal.desktop', 'unity://running-apps', 'unity://devices']
Run Code Online (Sandbox Code Playgroud)

2. 移除和附加到 Unity Launcher 的可能方法

最简单的例子已经展示了 - 通过gsettings实用程序。删除和附加特定项目需要解析gsettings输出。这可以通过sedawk实用程序来完成,甚至在bash. 但是,我发现 python 允许更简单的方法和“阻力最小的路径”。因此,此处提供的示例gsettings与 python 一起使用。

这是一个移除的案例:

$ gsettings get com.canonical.Unity.Launcher favorites|                                                                               
> python -c 'import ast,sys; x =[]; x = [i for l in sys.stdin for i in ast.literal_eval(l)]; 
> x.pop(x.index("application://"+sys.argv[1])); print x' firefox.desktop
['application://gnome-terminal.desktop', 'unity://running-apps', 'unity://devices']
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?我们将gsettings getvia管道的输出传递给python。Python 然后读取标准输入流并使用ast库评估列表的文本表示,并将其转换为 Python 可以识别的实际列表。这大大简化了工作——如果这是 awk 或 sed,我们将不得不处理删除和附加单个字符。最后,我们sys.argv[1]通过在列表中找到它的索引来删除(弹出)第二个命令行参数(由 指示)。我们现在有了新的列表,可以通过管道进一步传递到gsettings set

完整的命令是这样的:

$ gsettings get com.canonical.Unity.Launcher favorites|
> python -c 'import ast,sys; x =[]; x = [i for l in sys.stdin for i in ast.literal_eval(l)]; 
> x.pop(x.index("application://"+sys.argv[1])); print "\""+repr(x)+"\""' firefox.desktop |
> xargs -I {} gsettings set  com.canonical.Unity.Launcher favorites {}
Run Code Online (Sandbox Code Playgroud)

可以很好地放入~/.bashrc这样的函数中:

remove_launcher_item()
{
  gsettings get com.canonical.Unity.Launcher favorites|
  python -c 'import ast,sys; x =[]; x = [i for l in sys.stdin for i in ast.literal_eval(l)];\
  x.pop(x.index("application://"+sys.argv[1])); print "\""+repr(x)+"\""' "$1" |
  xargs -I {} gsettings set  com.canonical.Unity.Launcher favorites {}
}
Run Code Online (Sandbox Code Playgroud)

这里需要注意的一些事情是,我们需要再次打印括在引号中的列表的“字符串”表示,并通过xargs. 附加的想法是相似的,除了pop我们使用append函数:

append_launcher_item()
{
  gsettings get com.canonical.Unity.Launcher favorites|
  python -c 'import ast,sys; x =[]; x = [i for l in sys.stdin for i in ast.literal_eval(l)];\
  x.append("application://"+sys.argv[1]); print "\""+repr(x)+"\""' "$1" |
  xargs -I {} gsettings set  com.canonical.Unity.Launcher favorites {}

}
Run Code Online (Sandbox Code Playgroud)

示例运行:

$ get_launcher                                                                                                                        
['unity://running-apps', 'unity://devices', 'application://firefox.desktop']
$ append_launcher_item gnome-terminal.desktop                                                                                         
$ get_launcher                                                                                                                        
['unity://running-apps', 'unity://devices', 'application://firefox.desktop', 'application://gnome-terminal.desktop']
$ 
Run Code Online (Sandbox Code Playgroud)

这些函数不一定必须是~/.bashrc. 您也可以将它们放入脚本中

3.launcherctl.py 实用程序

随着时间的推移,我在 python 中研究并构建了一组可以有效执行与gsettings实用程序相同的功能。将 Python 的强大功能与这些功能结合在一起,我创造了launcherctl.py实用程序。

这是一项正在进行的工作,将来会扩展以包含更多功能。对于这个特定问题,我将保留第一个版本中出现的源代码。更多版本和改进可以在GitHub找到

与 bash 函数相比,此脚本有哪些优点?1. 这是一个具有特定目的的“中心化”实用程序。您不必为每个操作设置单独的脚本/函数。2. 使用简单,命令行选项极简 3. 与其他实用程序结合使用时,这提供了更具可读性的代码。

用法

-h命令行选项所示:

$ ./launcherctl.py -h                                                                                                                 
usage: launcherctl.py [-h] [-f FILE] [-a] [-r] [-l] [-c]

Copyright 2016. Sergiy Kolodyazhnyy.
    This command line utility allows appending and removing items
    from Unity launcher, as well as listing and clearing the
    Launcher items.

    --file option is required for --append and --remove 


optional arguments:
  -h, --help            show this help message and exit
  -f FILE, --file FILE
  -a, --append
  -r, --remove
  -l, --list
  -c, --clear
Run Code Online (Sandbox Code Playgroud)

命令行使用很简单。

附加:

$ ./launcherctl.py -a -f wechat.desktop
Run Code Online (Sandbox Code Playgroud)

移动:

$ ./launcherctl.py -r -f wechat.desktop
Run Code Online (Sandbox Code Playgroud)

完全清除启动器:

$ ./launcherctl.py -c 
Run Code Online (Sandbox Code Playgroud)

在启动器上列出项目:

$ ./launcherctl.py -l                                                                                                                 
chromium-browser.desktop
firefox.desktop
opera.desktop
vivaldi-beta.desktop
Run Code Online (Sandbox Code Playgroud)

如前所述,它可以与其他命令一起使用。例如,从文件添加:

$ cat new_list.txt                                                                                                                    
firefox.desktop
wechat.desktop
gnome-terminal.desktop    
$ cat new_list.txt | xargs -L 1 launcherctl.py -a -f
Run Code Online (Sandbox Code Playgroud)

同样可以用于删除从文本文件中给出的项目

dash按钮中删除第三项:

$ launcherctl.py  -l | awk 'NR==3' | xargs -L 1 launcherctl.py -r -f  
Run Code Online (Sandbox Code Playgroud)

获取源代码和安装

手动方式:

  1. 创建一个目录~/bin
  2. 将下面的源代码保存到文件中 ~/bin/launcherctl.py
  3. 如果您是bash用户,则可以使用 source~/.profile或注销并登录。该~/bin目录将自动添加到您的$PATH变量中。对于那些不使用 的人bash,请将其添加~/bin$PATHshell 配置文件中的变量中,如下所示:PATH="$PATH:$HOME/bin

正如我提到的,代码的最新更改进入了 GitHub 存储库。如果你已经git安装了,步骤就更简单了:

  1. git clone https://github.com/SergKolo/sergrep.git ~/bin/sergrep
  2. echo "PATH=$PATH:$HOME/bin/sergrep" >> ~/.bashrc
  3. source ~/.bashrc. 在此步骤之后,您可以launcherctl.py像任何其他命令一样调用。获取更新非常简单cd ~/bin/sergrep;git pull

从 GitHub 获取代码,无需git

  1. cd /tmp
  2. wget https://github.com/SergKolo/sergrep/archive/master.zip
  3. unzip master.zip
  4. 如果你没有~/bin,那就用mkdir ~/bin
  5. mv sergrep-master/launcherctl.py ~/bin/launcherctl.py

在所有情况下,都适用相同的规则 - 脚本必须位于添加到PATH变量的目录中,并且必须具有设置的可执行权限chmod +x launcherctl.py


原始源代码

$ gsettings get  com.canonical.Unity.Launcher favorites
['application://wps-office-et.desktop', 'application://wps-office-wpp.desktop', 'application://wps-office-wps.desktop', 'unity://running-apps', 'unity://devices']
$ gsettings set  com.canonical.Unity.Launcher favorites  "['wechat.desktop']"                                                         
$ gsettings get  com.canonical.Unity.Launcher favorites                                                                               
['application://wechat.desktop', 'unity://running-apps', 'unity://devices']
Run Code Online (Sandbox Code Playgroud)

补充说明: