Here’s are a few short tips - to toggle Dark Mode on macOS without opening System Preferences. Useful when designing your Dark Mode enabled web site using prefers-color-sheme Media Query, like I did.

Toggle Dark Mode Via an Automator Application

  1. Run Automator.app.
  2. If the new file dialog opens, select Type Application, else File > New... > Application.
  3. From the Actions list, select Library > Utilities and double click Change System Appearance:

Automator - Change System Appearance

  1. File > Save... - I saved it as darkmode.app on my Desktop.
  2. When you first run darkmode.app, you’ll be greeted with this message - hit Ok to grant access:

Allow Access to System Events.app

Toggle Dark Mode Via Touch Bar Quick Action

  1. Run Automator.app.
  2. If the new file dialog opens, select Type Quick Action, else File > New... > Quick Action.
  3. From the Actions list, select Library > Utilities and double click Change System Appearance.
  4. File > Save... - I saved it as Dark Mode
  5. Head over to your System Preferences > Keyboard and hit Customise Touch Bar...
  6. Drag Quick Actions on to your Touch Bar.
  7. Now select the Quick Actions icon on your Touch Bar.
  8. When you first run the Quick Action, you’ll be greeted with a similar message to the one above except for “ServicesUIAgent” - again hit Ok.

Toggle Dark Mode Via Terminal or Shell Script

  1. Open Terminal.app.
  2. Either, just run this from the terminal or save this snippet as a shell script:
    osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'
  3. Or, edit your .bash_profile and include an alias darkmode:
    alias darkmode="osascript -e 'tell application \"System Events\" to tell appearance preferences to set dark mode to not dark mode'"
  4. Once again, accept the security prompt.

Closing

One last thing, if you want to undo the Allow permission:

  1. Head over to System Preferences > Security & Privacy > Automation.
  2. Hit the lock icon and enter your password to make changes.
  3. Un-check to revoke permissions to System Events.app:

System Preferences Security and Privacy Automation System Events