Skip to main content
100%

toggle dark mode: 🔋 → 🌚 / 🔌 → ☀️

✓ Published0🌍 Public
Aabernier
Last edited Sep 4, 2023
Created on Sep 4, 2023

This example demonstrates a macOS automation script that toggles dark mode based on power source, checking every 60 seconds via a LaunchAgent plist. The Bash script uses `pmset` to detect whether the system draws from battery or AC power, and `defaults` to read the current appearance mode (Dark, Light, or Auto). It saves the original mode to a temporary file before enabling dark mode on battery, then reverts to the saved preference when power returns, using `osascript` to control System Events and display notifications. The code also handles Auto mode by writing the appropriate preference via `defaults write`.

AI-generated description

Every 60s: toggle dark mode if on battery (save previous value). If back on power, revert to previous value.

NB: You'll get a notification when changed.

INSTALL

  • ~/toggle_dark_mode.sh (don't forget to chmod u+x it)
  • ~/Library/LaunchAgents/com.user.toggle_dark_mode.plist

Similar vizzes