6

How can I toggle the system dark mode from Terminal without using AppleScript? I have already searched things like "zsh toggle dark mode" and "terminal set dark mode mac", but I can't find relevant results. Is there any way to do this? I'd like to do it without AppleScript unless it's absolutely necessary.

3 Answers 3

10

As far as I'm aware, there's no way to do this without AppleScript, but you can do it in one line using it:

osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to not dark mode' 

From https://brettterpstra.com/2018/09/26/shell-tricks-toggling-dark-mode-from-terminal/

1
  • You're largely correct, there, regarding AppleScript, unless you wish to use direct plist modifications, like Debashish M shows in his answer, below. The difference, here, being that the osascript command forcibly toggles the active state (meaning the Appearance panel in System Settings will NOT reflect it, and it'll reset on reboot), while the other changes the setting's value (meaning it's ACTUALLY changed and will persist). Personally, I favor your approach... so much so I included a tutorial, below, for how to set it up as a clickable dock icon and/or system-wide hotkey trigger. Commented Oct 9 at 0:47
0

Make sure all documents in other apps are saved and that other apps are closed, because you should restart your Mac at the end of this.

  1. Open Terminal.

  2. Run sudo defaults write /Library/Preferences/.GlobalPreferences.plist _HIEnableThemeSwitchHotKey -bool true

    (👆you can copy/paste this into your Terminal command line if you prefer).

  3. Because you're entering a sudo command, macOS will prompt you to enter your administrator password.. Enter it and hit return.

  4. Quit the Terminal app.

  5. Restart your Mac.

1
0

Adding a Dock Icon and/or System-Wide Hotkey to Toggle Modes

Just to expand on @shreyasm-dev's excellent (and correct!) response, above, with some convenience/quality-of-life addendums:

  1. Open Shortcuts - Open your Shortcuts App (this is built into MacOS).
  2. Add a Shortcut - At the top of the panel, click the âž• icon ("New Shortcut").

New Shortcut Icon

  1. Search AppleScript - Into the right-hand panel's search field, type "AppleScript".
  2. Add to Application - Double-click "Run AppleScript".

Run AppleScript

  1. Update Code - Into the resulting code input box, update it to read the following:
on run {input, parameters} tell application "System Events" to tell appearance preferences to set dark mode to not dark mode end run 

(Note that this code is nearly-identical to the osascript command @shreyasm-dev provided; you're simply omitting the shell osascript -e command, since we're providing the code directly to the script instead of evaluating it from within the shell.)

Completed code box

  1. Name and Save - At the top of the window, enter a name for the App (I used "ToggleDarkMode"). You can choose a default app icon, too, if you like ("I'm using the system "Appearance" icon; seems apropos.)

Name and Icon

  1. Set (Optional) Hotkey - You can then click the ⓘ icon in the window's top-right, followed by the Add Keyboard Shortcut button in the panel below it. Enter the hotkey you like - in my case, I use ⌘+⌥+⌃+D (Cmd+Opt+Ctrl+D).

Add Keyboard Shortcut

  1. Close Editor - Close the window (⌘+W / Cmd + W); it will have auto-saved as soon as you named it.

  2. (Optionally) Add to Dock - Finally, if you want to add this shortcut to your dock, simply right-click it, and select "Add to Dock".

Add to Dock

...which will create it as a .app inside of /Users/<your_username>/Applications/ToggleDarkMode.app, and add it to your dock.

In Dock

  1. (Optionally) Add/Move to Applications Menu/Fan - If you'd prefer to have it in your Applications fly-out menu instead, simply move the file from /Users/<your_username>/Applications/ to /Applications/, instead.

In Applications Menu

  1. (Optionally) Add to Menu Bar - If you'd rather have the action available as a Menu Bar icon...

In Menu Bar

...simply drag the completed Shortcut to the Menu Bar category on the left.Menu Bar Category

Final note: Not trying to take away any of the respect/glory from @shreyasm-dev, here. This is simply a different means of implementing the same effect in MacOS, from some means OTHER than the terminal (which, albeit, was not what OP asked for), for anyone stumbling on this later looking for a hotkey- or GUI-driven means of doing the same thing. <3

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.