MacOS – make the laptop tell me when battery is low even while “show battery status in menu bar” is not checked

automationmacosmojave

I unchecked "Show battery status in menu bar" in System Preferences > Energy Saver, because I use coconutBattery instead (and obviously I don't want to just re-check that).

But this means that now my MacBook doesn't alert me when the battery is getting low and just drains itself to 0% then dies on me.

I want to make an automatic AppleScript/cron job/whatever to alert me when the battery is less than 50%, and again when it's less than 20%.

How can I do that?

Best Answer

I had the same issue when I use a Bitbar plugin on OSX and have the default battery menubar unchecked. Here is what I use in my shell script to include a osascript call to create a notification pop up.

# Get info
power_source=( $(pmset -g batt | awk 'FNR == 1 {print $4$NF}' ) )
battery_level=( $(pmset -g batt | awk -F"\t" 'FNR == 2 {print $2}' | grep -o '[0-9]\+' ) )

# Notification when battery level is low
if [ "$power_source" != "'ACPower'" ] && [ "$battery_level" -le 5 ]; then osascript -e 'display notification "️⚠️ LOW POWER! ⚡" with title "Battery"';
fi

You could use this as a cron job. =)