Skip to content

Instantly share code, notes, and snippets.

@abernier
Last active September 7, 2023 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abernier/6ec6c675354cad560a4a613630a5e334 to your computer and use it in GitHub Desktop.
Save abernier/6ec6c675354cad560a4a613630a5e334 to your computer and use it in GitHub Desktop.
toggle dark mode: πŸ”‹ β†’ 🌚 / πŸ”Œ β†’ β˜€οΈ

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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.toggle_dark_mode</string>
<key>ProgramArguments</key>
<array>
<string>/Users/abernier/toggle_dark_mode.sh</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
</dict>
</plist>
#!/bin/bash
#
# Toggle dark-mode when on battery. It executes every 60s (see: ~/Library/LaunchAgents/com.user.toggle_dark_mode.plist)
#
# see: https://chat.openai.com/share/dd8837b6-de79-4f8b-8e69-ef5eeaa33732
#
#!/bin/bash
# Check if running on battery
power_source=$(pmset -g ps | grep 'Now drawing from' | awk '{print $4}' | tr -d "'")
# File to store the original mode (using mktemp)
original_mode_file="/tmp/original_mode"
# Check current appearance mode: Dark, Light, or Auto
is_dark=$(defaults read -g AppleInterfaceStyle 2>/dev/null)
auto_mode=$(defaults read -g AppleInterfaceStyleSwitchesAutomatically 2>/dev/null)
# Function to display macOS notifications
notify() {
osascript -e "display notification \"$1\" with title \"$(basename $0)\""
}
# Switch modes based on power source
if [ "$power_source" = "Battery" ]; then
echo "πŸ”‹"
# Save the original mode if not on Dark
if [ -n "$is_dark" ]; then
echo "Dark" > "$original_mode_file"
elif [ -n "$auto_mode" ]; then
echo "Auto" > "$original_mode_file"
else
echo "Light" > "$original_mode_file"
fi
# Enable Dark mode
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to true'
notify "Dark mode enabled to preserve πŸ”‹ (previous: $(cat $original_mode_file))"
else
echo "πŸ”Œ"
# Revert to the original mode if it was saved
if [ -f "$original_mode_file" ]; then
original_mode=$(cat "$original_mode_file")
case "$original_mode" in
"Light")
osascript -e 'tell app "System Events" to tell appearance preferences to set dark mode to false'
notify "Light mode reverted since you are back on πŸ”Œ"
;;
"Dark")
# Already in Dark Mode, no action needed
;;
"Auto")
defaults write -g AppleInterfaceStyleSwitchesAutomatically -bool true
notify "Auto mode reverted since you are back on πŸ”Œ"
;;
esac
# Delete the original_mode_file
rm "$original_mode_file"
fi
fi
@abernier
Copy link
Author

abernier commented Sep 7, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment