Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ariefrahmansyah/9ba8363943b58b4263f95ee0d11c206c to your computer and use it in GitHub Desktop.
Save ariefrahmansyah/9ba8363943b58b4263f95ee0d11c206c to your computer and use it in GitHub Desktop.
Battery Percentage Boundary Notification Background Script for macOS

Battery Percentage Boundary Notification Background Script for macOS

Preface

I'm weird. We all have our weird habits and quirks. Luckily for me, mine only involves my the battery in my macbook computer.

Are you worried about keeping your devices' batteries healthy and keeping a charge? With every device I have owned, battery health has degraded noticeably over time, likely due to my poor charging habits.

I'll be the first to admit, I am no electrochemist. I might be (and likely am) askew, and what I am about to show you may have absolutely no effect on battery performance. But, I like to believe it does :)

I wanted a way to get a notification when my battery reaches 40% and 80%. That's my target range. So I wrote a script to do it (I have no life). It is written in AppleScript, and uses a fancy-pants notification card to display a message when it's time to charge or unplug the charge cable. The script relies on Apple's task scheduler launchd, and by following these instructions, the script will run automatically after login.

Installation

First, you will need to download the two files below BatteryStatusNotification.scpt and battery.monitor.plist. Make sure you edit the plist file to point to the correct script file.

If you want to run the script once, just run osascript BatteryStatusNotification.scpt. This is fine, but won't automatically run the script on login. To do this, copy the plist file battery.monitor.plist to ~/Library/LaunchAgents/. I recommend placing the script under ~/Applications.

Test this out and let me know what you think! You can also easily adjust the boundaries, or modify the script to suit your needs.

Further Reading

Stack Exchange - How to run custom AppleScript in Background

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>battery-status-monitor.job</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/osascript</string>
<string>PATH/TO/YOUR/SCRIPT/FILE/BatteryStatusNotification.scpt</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
repeat
set chargeState to do shell script "pmset -g batt | awk '{printf \"%s %s\\n\", $4,$5;exit}'"
set percentLeft to do shell script "pmset -g batt | egrep -ow '([0-9]{1,3})[%]' | egrep -ow '[0-9]{1,3}'"
considering numeric strings
if chargeState contains "Battery Power" and percentLeft ≤ 40 then
display notification "Time to plug me in :)" with title "Battery Charge Boundary"
else if chargeState contains "AC Power" and percentLeft ≥ 80 then
display notification "Time to unplug me :)" with title "Battery Charge Boundary"
end if
end considering
delay 60
end repeat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment