【问题标题】:Python code to detect dark mode in OS X El Capitan to change the status bar menu icon在 OS X El Capitan 中检测暗模式以更改状态栏菜单图标的 Python 代码
【发布时间】:2016-09-03 16:25:20
【问题描述】:
我有客观的 C 代码来检测暗模式以更改状态栏:
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
同样,我们如何在 python 中做同样的事情?
【问题讨论】:
标签:
python
objective-c
macos
osx-elcapitan
pyobjc
【解决方案1】:
我不知道您是否可以直接在 python 中执行此操作。但至少你可以调用终端命令defaults read -g AppleInterfaceStyle。
目前它的行为是这样的:如果它的退出代码是 0,它会报告“暗模式”。如果为 1(错误),则可以假定为 light 模式。在我看来这不是很干净,但它可以工作并且可以从Java program 成功使用。
如何从 python 中生成一个新进程是另一个问题,已经是answered。
【解决方案2】:
无论您想检测模式(暗模式还是亮模式),请尝试以下这些行。
center = NSDistributedNotificationCenter.defaultCenter()
center.addObserver_selector_name_object_(self,"enableDarkMode",'AppleInterfaceThemeChangedNotification',None)
【解决方案3】:
在python中os模块可能会派上用场来检测模式。
基本上,我们使用python访问并运行终端命令在默认设置中找到AppleInterfaceStyle属性。
import os
has_interface = os.popen("defaults find AppleInterfaceStyle").read()
if not has_interface:
print("Use a light Style")
else:
interface_system = os.popen("defaults read -g AppleInterfaceStyle").read()
print("Interface Style:" + interface_system) # interface_system = 'Dark\n'