
我想建立一個鍵盤快捷鍵來切換單元啟動器的自動隱藏選項。基於答案如何以程式設計方式更改啟動器的隱藏行為我嘗試編寫一個 python 腳本來完成這項工作。然後我應該弄清楚如何使用鍵盤快捷鍵來運行它。
我的腳本如下圖所示:
#!/bin/python
AUTOHIDE=$(dconf read /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode)
if (AUTOHIDE==1):
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 0
else:
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 1
但是從終端運行腳本(執行 'python scriptname.py' )不起作用。我在 $ 符號處收到“無效語法”錯誤。
你要知道我對Python(或一般的編寫腳本)幾乎一無所知。 (我剛剛花了幾個小時在網路上搜尋幫助和範例)。
所以實際問題是:
- 我做錯了什麼?
- 我是否為此選擇了一種複雜的方法? 在這種情況下我怎樣才能更輕鬆地做到這一點?
答案1
如果你想以 Pythonic 的方式來做。
#!/bin/python
import subprocess
AUTOHIDE = subprocess.check_output (["/usr/bin/dconf", "read", "/org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode"])
if (AUTOHIDE==1):
subprocess.call (["/usr/bin/dconf", "write", "/org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode", "0"])
else:
subprocess.call (["/usr/bin/dconf", "write", "/org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode", "1"])
您必須透過建立子進程來執行程式。
這是 bash 腳本版本
#!/bin/bash
AUTOHIDE=$(dconf read /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode)
if [[ $AUTOHIDE -eq 1 ]]
then
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 0
else
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 1
fi
可以像這樣指派快捷方式這。
答案2
一種簡單的方法是建立自訂快捷方式。
存取“系統設定”>“鍵盤”>“快捷方式”>“自訂快捷方式”然後單擊“+”添加新快捷方式,並在命令框中貼上:
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 0
這將建立一個用於顯示啟動器的捷徑。現在要隱藏啟動器,您應該建立另一個快捷方式新增命令:
dconf write /org/compiz/profiles/unity/plugins/unityshell/launcher-hide-mode 1
當然,現在每個功能都會有一個命令,但我將它們並排放置,發現它非常直觀。
答案3
對於 Unity 2D,dconf 行應該是
/com/canonical/unity-2d/launcher/hide-mode
還有第三種模式“Intellihide”,其值為 2。