4

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.

4

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

-1

Just use the kill dock command when you have done your sudo ... command,

If you want this for automatic day night mode, what about using Automator and connect it with two daily events in your calendar...

  • 1
    I could definitely kill the Dock, thanks. However, it still has the disadvantage of my having to enter my password in; I'd rather not store it in the Automator file! – Skeleton Bow Feb 5 '18 at 18:54
  • You can use AppleScript, there is a workaround so you can save your admin password(unencrypted) in the script... – Harcker Feb 5 '18 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .