Skip to content

Instantly share code, notes, and snippets.

@abernier
Last active September 7, 2023 08:31
Show Gist options
  • Save abernier/6ec6c675354cad560a4a613630a5e334 to your computer and use it in GitHub Desktop.
Save abernier/6ec6c675354cad560a4a613630a5e334 to your computer and use it in GitHub Desktop.

Revisions

  1. abernier revised this gist Sep 4, 2023. No changes.
  2. abernier revised this gist Sep 4, 2023. No changes.
  3. abernier revised this gist Sep 4, 2023. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion README.md
    Original 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.
    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

  4. abernier created this gist Sep 4, 2023.
    6 changes: 6 additions & 0 deletions README.md
    Original 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`
    14 changes: 14 additions & 0 deletions com.user.toggle_dark_mode.plist
    Original 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>
    66 changes: 66 additions & 0 deletions toggle_dark_mode.sh
    Original 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