|
#!/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 |
https://twitter.com/abernier_/status/1698645537958883422?s=20