Coming from Windows where there's an entire set of configuration options to set Low Battery Warnings, Critical Battery Warnings and Actions, etc. I'm dismayed that macOS doesn't have any configuration at all, and only has a permanently fixed Low Battery notification at 10% and nothing else.
I have made a script (available on GitHub Gist) that will show a warning alert like this:
You can save the first file as /usr/local/bin/battcritical (without the .sh) and if you only want it in current session, just run it with:
nohup /usr/local/bin/battcritical &
To have it start warning you at another percentage (and below), change the number after -le on line 11 to your desired percentage.
In order to have it automatically run every time you boot up your Mac, also save the second file as /usr/local/bin/login-scripts (without the .sh), then drag and drop the login-scripts file into the Login Items list in your System Settings.
Remember to give both files the execute permission:
sudo chmod a+x /usr/local/bin/battcritical
sudo chmod a+x /usr/local/bin/login-scripts
Scripts embedded below for reference:
#!/bin/bash | |
# Initialize the last_battery_percentage | |
last_battery_percentage=-1 | |
while true; do | |
# Get the current battery percentage | |
battery_percentage=$(pmset -g batt | grep -o '[0-9]\+%' | tr -d '%') | |
# Only display the dialog if the new battery percentage is 5% or less | |
if [[ $battery_percentage -le 5 ]]; then | |
# Only display the dialog if the computer is on battery power | |
power_source=$(pmset -g batt | grep -o 'discharging') | |
if [[ $power_source == "discharging" ]]; then | |
# Check if the battery percentage has changed | |
if [[ $last_battery_percentage -ne $battery_percentage ]]; then | |
# afplay /System/Library/Sounds/Glass.aiff > /dev/null 2>&1 | |
osascript <<EOF > /dev/null 2>&1 | |
beep | |
display alert "Battery Level: ${battery_percentage}%" message "Connect a charger soon!" as critical giving up after 60 | |
EOF | |
# Update the last battery percentage | |
last_battery_percentage=$battery_percentage | |
fi | |
else | |
# Reset the last battery percentage (shows message again if unplugged) | |
last_battery_percentage=-1 | |
fi | |
fi | |
# Sleep for 1 second before checking again | |
sleep 1 | |
done |
nohup battcritical & | |
nohup osascript -e 'tell application "Terminal" to quit' & |
~-~
Comments
Post a Comment
Comments are moderated, and are usually posted within 24 hours if approved. You must have a minimum of OpenID to post comments.