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.

1 Answer 1

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

3
  • Now can we make an application that switches Dark Mode on and off based on sunrise and sunset?
    – JBis
    Jun 5, 2018 at 2:24
  • @Josh it won't be hard to fork my demo app and add a Timer to check the system time every minute, then compare that to a daily result from Sunrise-Sunset.org, and then use that to set AppleInterfaceStyle.current
    – Ky -
    Jun 8, 2018 at 15:17
  • @JBis ApolloZhu has created Dynamic Dark Mode, which does exactly this. You can even install it using Homebrew!
    – Ky -
    Mar 13, 2019 at 22:32

You must log in to answer this question.

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