Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save prishs/01fd4d4b5c8e796a0ee8528e632f7546 to your computer and use it in GitHub Desktop.
Save prishs/01fd4d4b5c8e796a0ee8528e632f7546 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

launchctl load -w ~/Library/LaunchAgents/battery.monitor.plist
launchctl list | less
launchctl unload -w ~/Library/LaunchAgents/battery.monitor.plist
<?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
if (percentLeft mod 5 = 0) then
try
--say "Battery Low"
do shell script "afplay /System/Library/Sounds/Blow.aiff"
--display notification "Time to plug me in 🔌" with title "Battery Charge Boundary" sound name "Blow"
--beep
end try
end if
display notification "Time to plug me in 🔌" with title "Battery Charge Boundary"
my clear_notifications()
else if chargeState contains "AC Power" and percentLeft ≥ 80 then
display notification "Time to unplug me 🔋" with title "Battery Charge Boundary"
if (percentLeft mod 5 = 0) then
try
--say "Battery High"
do shell script "afplay /System/Library/Sounds/Blow.aiff"
--display notification "Time to unplug me 🔋" with title "Battery Charge Boundary" sound name "Blow"
--beep
end try
end if
my clear_notifications()
end if
end considering
delay 60
end repeat
on clear_notifications()
tell application "System Events"
tell application process "NotificationCenter"
try
--set content to (value of static text 1 of group 1 of UI element 1 of scroll area 1 of window 1) as text
if value of static text 1 of group 1 of UI element 1 of scroll area 1 of window 1 is "Battery Charge Boundary" then
delay 4.0
perform (first action of group 1 of UI element 1 of scroll area 1 of window 1 where description is "Close")
end if
-- display notification content with title "xyz"
end try
end tell
end tell
end clear_notifications
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment