Skip to content

Instantly share code, notes, and snippets.

@YashKumarVerma
Created December 3, 2023 16:54
Show Gist options
  • Save YashKumarVerma/db21bdc78310e1634de67d72c5d39b82 to your computer and use it in GitHub Desktop.
Save YashKumarVerma/db21bdc78310e1634de67d72c5d39b82 to your computer and use it in GitHub Desktop.
macos script to remind to pull out the charger
#!/bin/bash
# add this command to cron by adding this line in the file open by `crontab -e`
# `* * * * * /Users/yash/scripts/cron/battery.sh`
PATH=/usr/bin:/bin # Set the PATH to include necessary directories
export PATH
# Full paths to commands
system_profiler_path=$(which system_profiler)
osascript_path=$(which osascript)
charging_status=$(/usr/sbin/system_profiler SPPowerDataType | grep "Charging" | awk '{print $2}' | head -n1)
battery=$(/usr/sbin/system_profiler SPPowerDataType | grep 'State of Charge' | awk '{print $5}')
echo "Battery Level: $battery%"
echo "Charging Status: $charging_status"
if [ "$charging_status" == "Yes" ] && [ "$battery" -gt 95 ]; then
notification_message="Battery level is high: $battery% - Charging"
"$osascript_path" -e "display notification \"$notification_message\" with title \"Battery Notification\" subtitle \"High Battery\" sound name \"Submarine\""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment