Last Updated: January 13, 2019
·
1.664K
· aspartango

Toggle OS X Dark Mode using Javascript

Script Editor

Open /Applications/Utilities/Script Editor.app

var darkMode = Application("System Events").appearancePreferences.darkMode;

darkMode.set(!darkMode.get());

Picture

You can build an application from this code using File > Export...and then use it as part of any other automation scheme (e.g., Login, Calendar, Keyboard Shortcuts).

How it works:

As of OS X 10.10 Yosemite, you can use javascript to automate application and system tasks.

You can play around with the OS X javascript REPL using osascript:

osascript -l JavaScript -i

But really, you should work in Script Editor, because it has all the documentation and library methods built-in.

/Applications/Utilities/Script Editor.app

Check out Window > Library for a list of all the applications you can work with.

OS X 10.10 also comes with a new Dark Mode, which can be toggled in System Preferences > General.

We can access this configuration option through javascript by getting the "System Events" application and modifying the appearance preference option for dark mode.

The process mirrors the UI.