
我編寫了一個安裝腳本來自動執行我的安裝過程。之後我希望它重新排列/新增/刪除統一啟動器的圖示。
使用dconf watch /
我可以在重新排序圖示時看到輸出。有沒有命令列方法可以做到這一點 - 可能是使用gsettings
?
答案1
介紹
下面的簡單腳本允許將文件作為參數,並將啟動器設定為具有文件中出現的程式的任何快捷方式(每行一個)。
基本想法是啟動器圖示實際上是.desktop
文件的連結(無論它們出現在哪裡),設定啟動器項目的規範方法是運行以下命令:
gsettings set com.canonical.Unity.Launcher favorites "[ 'item1.desktop' , 'item2.desktop' , . . . 'item3.desktop; ]"
如果您想要添加許多項目並且引用可能會變得很痛苦,這可能會變得乏味。人們始終可以打開所需的程式並右鍵單擊該圖標以調用“鎖定到啟動器”選項,但當您處理大量項目時,這也是不切實際的。
這裡採取的方法是簡單地讀取一個文件,每行讀取一次,建立命令文本,然後執行它。這與執行上面的命令沒有什麼不同gsettings set
,只不過辛苦的工作已經為您完成了。
用法:
若要運行腳本,請將其儲存到檔案中,使其可執行chmod +x /path/to/script
並運行為
python /path/to/script /path/to/file
輸入檔案應包含要新增至啟動器的專案的完整路徑,例如/usr/share/applications/firefox.desktop
,但這不是必需的,一行firefox.desktop
也可以。
示範
運行腳本之前
運行腳本後
請注意,順序與輸入檔中出現的條目完全相同
腳本來源
#!/usr/bin/env python
# Author: Serg Kolo
# Date: April 22, 2016
# Purpose: programmatically set Unity launcher items
# by reading a file
# Written for: http://askubuntu.com/q/760895/295286
# Tested on: Ubuntu 14.04 LTS
import sys
import subprocess
command="""gsettings set com.canonical.Unity.Launcher favorites """
def run_command(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
output = p.stdout.read().strip()
return output
items=""
with open(sys.argv[1]) as file:
for line in file:
temp = "'" + line.strip().split('/')[-1] + "'"
items = ",".join([items,temp])
items = '"[ ' + items[1:] + ' ]"'
print run_command(command + " " + items)