Shell Tricks: Toggling Dark Mode from Terminal
If you’ve upgraded to Mojave, you’ve probably tried out Dark Mode. Maybe you love it. I got over the whole idea years ago, back when you had to run hacks just to change the menu bar background. I like my light interface.
I’m not grumpy about it, but it means I have to test any UIs I create in both modes now, so obviously that needs some automation.
Dark Mode can be toggled with AppleScript via System Events.
tell application "System Events"
tell appearance preferences
set dark mode to true
end tell
end tell
You can use the read value of “dark mode” as a condition (negated boolean) to create a toggle, turning it off if it’s on, on if it’s off.
tell application "System Events"
tell appearance preferences
set dark mode to not dark mode
end tell
end tell
All of this can be condensed to a one-liner for use with osascript
from any script or as an alias for your shell:
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode'
With these examples you can include a Dark Mode switcher in a script, an automated test, an Alfred/Launchbar action, a Keyboard Maestro shortcut, or create a Script Editor applet for a double-clickable mode switcher.
I’m assuming normal citizens won’t have a need to toggle between the two modes frequently, but for those who do, enjoy.
Ryan Irelan has produced a series of shell trick videos based on BrettTerpstra.com posts. Readers can get 10% off using the coupon code TERPSTRA
.