简体   繁体   English

用于在OS X El Capitan中检测黑暗模式的Python代码,以更改状态栏菜单图标

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

I have objective C code to detect dark mode to change the status bar: 我有客观的C代码来检测暗模式以更改状态栏:

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

Similarly, how can we do the same in python? 同样,我们如何在python中做同样的事情?

I don't know if you can do this directly from within python. 我不知道您是否可以直接在python中执行此操作。 But at least you can invoke the terminal command defaults read -g AppleInterfaceStyle . 但是至少您可以调用defaults read -g AppleInterfaceStyle的终端命令。

Currently its behavior is like this: If its exit code is 0, it reports "dark mode". 当前,它的行为是这样的:如果其退出代码为0,则报告“暗模式”。 If it is 1 (error), you can assume light mode. 如果为1(错误),则可以采用灯光模式。 This isn't very clean in my opinion, but it works and is used successfully from a Java program . 我认为这不是很干净,但是它可以正常工作,并且可以在Java程序中成功使用。

How to spawn a new process from within python is a different question, which has already been answered . 如何从python内部生成新进程是一个不同的问题,该问题已得到解答

Try these following lines wherever you want to detect the mode (dark mode or light mode). 无论您要检测模式(暗模式还是亮模式),请尝试以下几行。

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

In python os module may come in handy to detect the mode. 在python中, os模块可能会派上用场来检测模式。

Basically, we use python to access and run terminal command to find AppleInterfaceStyle property in default settings. 基本上,我们使用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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM