Enable macOS "Dark menu bar and Dock" without password or restarting

Is there a way to programmatically enable the macOS "Dark menu bar and Dock" option (found in System Preferences > General) via a Terminal command, without having to restart the Mac?

There is a way to change this option in the Terminal, but it requires the use of sudo and seems to require a restart, neither of which I want to use.

I could do this via the Terminal using AppleScript, but as far as I can tell it would require GUI scripting and my having to allow Terminal to control the Mac in the Accessibility settings.


Unfortunately, changing system preference files will always require administrative permissions, which is something the user has to explicitly give your script.

Fortunately, there is a way to accomplish this without changing system preference files! Using AppleScript, one can ask the system event daemon to change the system appearance.

I'm testing this on my work laptop, which doesn't allow me to change Accessibility preferences, so I don't even know if Terminal is enabled there (I suspect it's not). Additionally, this does not do any GUI scripting! (I recorded the below gif with System Preferences not running) Instead, it performs the same action that checking the box in System Preferences performs, but without needing access to that checkbox.

Hope this works for you!

AppleScript

-- Toggle dark mode
tell application "System Events" to tell appearance preferences to set dark mode to not dark mode

-- Enable dark mode, even it it's already enabled
tell application "System Events" to tell appearance preferences to set dark mode to true

-- Disable dark mode, even it it's already disabled
tell application "System Events" to tell appearance preferences to set dark mode to false

Bash

Don't wanna have something kick off an AppleScript? I'm with you. You can execute any AppleScript from bash (and thus the terminal) passing it as a string to the osascript -e command:

# Toggle dark mode
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'

# Enable dark mode, even it it's already enabled
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to true'

# Disable dark mode, even it it's already disabled
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to false'


I have made a proof-of-concept app that shows off this technique, plus a couple others:

macOS Dark Mode Switcher

Example of the above app at work