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.
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.
Stack Exchange - How to run custom AppleScript in Background
Here's some code that seems to accomplish what I described above:
Notes:
delay
after theperform
operation there seems to be a race condition between the last close completing and the next close being triggered, which halts operation. Other related code I've found online usesdelay
as well (1, 2). Though0.5
is pretty conservative; you could probably get away with0.2
on a new or fast machinedelay
in the mainrepeat
block, since having to wait a minute for notification dismissal is pretty long; though the shorter thedelay
the more this script will impact your machine's battery & performance