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

3

1

I have objective C code to detect dark mode to change the status bar:

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

Similarly, how can we do the same in python?

Binni answered 9/5, 2016 at 6:19 Comment(0)
B
1

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)
Buckskin answered 20/5, 2016 at 9:58 Comment(0)
B
1

I don't know if you can do this directly from within python. But at least you can invoke the terminal command defaults read -g AppleInterfaceStyle.

Currently its behavior is like this: If its exit code is 0, it reports "dark mode". If it is 1 (error), you can assume light mode. This isn't very clean in my opinion, but it works and is used successfully from a Java program.

How to spawn a new process from within python is a different question, which has already been answered.

Babbling answered 10/5, 2016 at 9:24 Comment(0)
B
1

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)
Buckskin answered 20/5, 2016 at 9:58 Comment(0)
D
0

In python os module may come in handy to detect the mode.

Basically, we use python to access and run terminal command to find AppleInterfaceStyle property in default settings.

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'
Develop answered 12/1, 2019 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.