brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: one osascript line switches your Mac between light and dark mode from the terminal

Terminal
~/tips/tip-terminal-osascript-dark-mode

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

Toggles macOS dark mode with a command you can bind or schedule.

Switching appearance means a trip into System Settings. AppleScript can flip it from the terminal, which also makes it easy to put behind a shortcut.

System Events exposes the appearance setting. This toggles it:

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

To read the current state, for a script that should act differently in the dark:

osascript -e 'tell application "System Events" to tell appearance preferences to get dark mode'

It answers true or false. Setting it to true or false instead of not dark mode gives you fixed commands, one for each mode.

I’d keep the toggle as a shell alias. You can also paste the AppleScript part into a Run AppleScript action in Shortcuts and give that a keyboard shortcut.

The first time, macOS asks whether Terminal may control System Events. Allow it, or the command fails with a not authorised error. You can change that later under Privacy & Security, Automation.

Further reading: osascript man page (macOS), Mac Automation Scripting Guide (Apple)

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link