Skip to content

Instantly share code, notes, and snippets.

@j33ty
Created March 25, 2019 16:57
Show Gist options
  • Save j33ty/b08a5d560d206f5c0a6a51a3bf79edde to your computer and use it in GitHub Desktop.
Save j33ty/b08a5d560d206f5c0a6a51a3bf79edde to your computer and use it in GitHub Desktop.
Get notification on Mac for low battery
#!/bin/bash
# Add this function to cronjob
# This will it every 2 minutes: "2 * * * * /path/to/script"
# Define a function to show the notification on MacOS
function notify() {
if [ $# -eq 3 ]; then
osascript -e "display notification \"$2\" with title \"$1\" sound name \"$3\""
fi
}
# Get the battery data and call notify function. Sound can be selected from /System/Library/Sounds
battery_percentage=$(pmset -g batt | grep -Eo "\d+%" | cut -d% -f1)
if (( battery_percentage > 10 )); then
notify "Low Battery" "Battery below 10%. Run for the charger!" "Tink.aiff"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment