Python 检测深色模式的代码 OS X El Capitan 更改状态栏菜单图标

Python code to detect dark mode in OS X El Capitan to change the status bar menu icon

我有 objective C 代码来检测暗模式以更改状态栏:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];

同样,我们如何在python中做同样的事情?

我不知道您是否可以直接从 python 中执行此操作。但至少你可以调用终端命令 defaults read -g AppleInterfaceStyle.

目前它的行为是这样的:如果它的退出代码是0,它报告"dark mode"。如果为 1(错误),则可以采用轻模式。在我看来这不是很干净,但它可以工作并且在 .

中成功使用

如何从 python 中生成新进程是一个不同的问题,已经 answered

在您想要检测模式(暗模式或亮模式)的任何位置尝试以下几行。

center = NSDistributedNotificationCenter.defaultCenter()
center.addObserver_selector_name_object_(self,"enableDarkMode",'AppleInterfaceThemeChangedNotification',None)

在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'