按字母顺序排序Unity Launcher图标

我喜欢按字母顺序排列锁定的Launcher应用程序,我想知道是否可以编写一个对它们进行排序的脚本,所以每次添加新程序时我都不必自己动手。

如果某个文件夹包含图标的所有名称和位置,我应该能够从中找到它。

保持启动器按字母顺序排序

在后台运行下面的脚本(请参阅下面的进一步操作)将使您的启动器按字母顺序从a到z排序:

运行脚本之前的前三个启动项: Gnome-terminal,Virtualbox,LibreOffice

在此处输入图像描述

运行脚本: dconf-editor,Files,Firefox

在此处输入图像描述

脚本的function

应用程序的(接口 – )名称在.desktop文件中定义。 这些文件可以存储在/usr/share/applications~/.local/share/applications 。 如果存在本地的那个,后者“否决”全球的那个。

该脚本检查启动器列表中的更改,运行(在循环中,每两秒)该命令:

 gsettings get com.canonical.Unity.Launcher favorites 

这将返回一个启动器项列表,其顺序与启动器的当前顺序相同。

如果启动器发生更改(例如,如果添加新的启动器),脚本将在.desktop文件中查找引用接口名称(保留本地.desktop文件的优先级)并根据这些接口名称对启动器项进行排序。

随后,脚本通过以下命令设置排序的启动器:

 gsettings set com.canonical.Unity.Launcher favorites "" 

如何使用

  1. 将下面的脚本复制到一个空文件中,将其另存为sort_launcher.py
  2. 通过运行(在终端中)命令来测试它:

     python3 /path/to/sort_launcher.py 
  3. 如果工作正常,请将其添加到启动应用程序:Dash> Startup Applications>添加命令:

     /bin/bash -c "sleep 15&&python3 /path/to/sort_launcher.py" 

剧本

 #!/usr/bin/env python3 import subprocess import os import time home = os.environ["HOME"] # define the two directories to search .desktop files, the string to identify application launchers dir1 = "/usr/share/applications/"; dir2 = home+"/.local/share/applications/"; s = "application://" # the key to read / write the launcher lget = "gsettings get "; lset = "gsettings set "; key = "com.canonical.Unity.Launcher favorites" # read current launcher def read_current(): return eval(subprocess.check_output(["/bin/bash", "-c", lget+key]).decode("utf-8").strip()) def sort_launcher(launchers): # split up launcher items in applications / others apps = []; others = [] for it in launchers: apps.append(it) if it.startswith(s) else others.append(it) # create a sorted application (launcher-) list sort_list = [] app_launchers = [] for item in apps: check = get_interfacename(item) if check: app_launchers.append([check, item]) app_launchers.sort(key=lambda x: x[0]) # set the sorted launcher launcher_set = [item[1] for item in app_launchers]+others subprocess.Popen(["/bin/bash", "-c", lset+key+' "'+str(launcher_set)+'"']) def get_interfacename(item): name = [] for dr in [dir1, dir2]: subject = dr+item.replace(s, "") try: app = [l.strip() for l in open(subject).readlines() if l.startswith("Name=")][0] name.append(app) except FileNotFoundError: pass else: name.append(app) return name[-1].strip("Name=").lower() # check every two seconds for changes, update launcher if necessary current_launcher1 = read_current() sort_launcher(current_launcher1) while True: time.sleep(2) current_launcher2 = read_current() if current_launcher1 != current_launcher2: sort_launcher(current_launcher2) else: pass current_launcher1 = current_launcher2 

笔记

  • 该脚本按应用程序的美英名称排序
  • 在系统监视器中,脚本显示cpu 0% ,这意味着该脚本几乎不会增加处理器负载。

你可以使用它们

 gsettings get com.canonical.Unity.Launcher favorites 

并设置它们

 gsettings set com.canonical.Unity.Launcher favorites VALUE 

其中VALUE的格式与你从gsettings get ...格式相同gsettings get ...