Last active
September 7, 2023 08:31
-
-
Save abernier/6ec6c675354cad560a4a613630a5e334 to your computer and use it in GitHub Desktop.
Revisions
-
abernier revised this gist
Sep 4, 2023 . No changes.There are no files selected for viewing
-
abernier revised this gist
Sep 4, 2023 . No changes.There are no files selected for viewing
-
abernier revised this gist
Sep 4, 2023 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,7 @@ 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 -
abernier created this gist
Sep 4, 2023 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ Every 60s: toggle dark mode if on battery (save previous value). If back on power, revert to previous value. ## INSTALL - `~/toggle_dark_mode.sh` (don't forget to `chmod u+x` it) - `~/Library/LaunchAgents/com.user.toggle_dark_mode.plist` This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ <?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> This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,66 @@ #!/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