下面的脚本将在“ nothing”和“ suspend”之间切换关闭动作:
#!/usr/bin/env python3
import subprocess
key = ["org.gnome.settings-daemon.plugins.power",
"lid-close-ac-action", "lid-close-battery-action"]
currstate = subprocess.check_output(["gsettings", "get",
key[0], key[1]]).decode("utf-8").strip()
if currstate == "'suspend'":
command = "'nothing'"
subprocess.Popen(["notify-send", "Lid closes with no action"])
else:
command = "'suspend'"
subprocess.Popen(["notify-send", "Suspend will be activated when lid closes"])
for k in [key[1], key[2]]:
subprocess.Popen(["gsettings", "set", key[0], k, command])
...并通知当前设置的状态是什么:
如何使用
只是:
说明
可以通过以下命令检索闭合动作设置的当前状态
gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action
(通电),并且
gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action
(电池)
该脚本读取当前状态,并使用以下命令设置相反的状态(“ suspend” /“ nothing”):
gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action '<action>'
可选地(附加地)
可选地/附加地,您可以将指示器作为检测器运行,以显示盖设置的当前状态。它会显示:
...在面板上,如果可以防止在关闭盖子时悬挂,则如果没有,它将显示为灰色。
剧本
#!/usr/bin/env python3
import subprocess
import os
import time
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
from threading import Thread
key = ["org.gnome.settings-daemon.plugins.power",
"lid-close-ac-action", "lid-close-battery-action"]
currpath = os.path.dirname(os.path.realpath(__file__))
def runs():
# The test True/False
return subprocess.check_output([
"gsettings", "get", key[0], key[1]
]).decode("utf-8").strip() == "'suspend'"
class Indicator():
def __init__(self):
self.app = 'show_proc'
iconpath = currpath+"/nocolor.png"
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
self.update = Thread(target=self.check_runs)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
def check_runs(self):
# the function (thread), checking for the process to run
runs1 = None
while True:
time.sleep(1)
runs2 = runs()
# if there is a change in state, update the icon
if runs1 != runs2:
if runs2:
# set the icon to show
GObject.idle_add(
self.indicator.set_icon,
currpath+"/nocolor.png",
priority=GObject.PRIORITY_DEFAULT
)
else:
# set the icon to hide
GObject.idle_add(
self.indicator.set_icon,
currpath+"/green.png",
priority=GObject.PRIORITY_DEFAULT
)
runs1 = runs2
def create_menu(self):
menu = Gtk.Menu()
# quit
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.stop)
menu.append(item_quit)
menu.show_all()
return menu
def stop(self, source):
Gtk.main_quit()
Indicator()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()
如何使用
- 将上面的脚本复制到一个空文件中,另存为
show_state.py
复制以下两个图标(右键单击->另存为),然后将它们保存在与相同的目录下show_proc.py
,并按如下所示精确命名
green.png
nocolor.png
现在,show_state.py
通过以下命令进行测试:
python3 /path/to/show_state.py
并通过设置此答案第一部分的快捷方式更改当前状态。
如果一切正常,请将以下内容添加到启动应用程序:
/bin/bash -c "sleep 15 && python3 /path/to/show_state.py"
注意
上方的检测器指示器是该答案的编辑版本。只需在功能改变测试runs()
(任选与根据面板图标),你可以用它来显示的状态什么是True
或False
。