A Growl Hack to Remind You to Unplug Your Charger
24 April, 2008 – 2:42 pmIf you’ve been using a Macbook Pro or similar for some time, you’ll find that their batteries need tender love and care, if they are to go the distance. According to Apple, I fit into the ‘ideal use’ category as I regularly use my notebook on the 1+ hour daily train commute.
However as the recent purchase of a new $199AUD battery has taught me, I often neglect to ‘unplug’ from the charger when weaseling away at work.
ioreg is part of Apple’s I/O Kit Registry, which you can use to determine programatically what the remaining charge of your battery is based on current and max capacity. Using this tool and Growl notifications, I’ve written a quick hack to remind me to unplug whenever my battery charge is greater than 99%.

First thing I did was write an applescript that runs a terminal command in the background, enumerates the current and max capacity, and then formats a Growl message if you’ve exceeded 99% charge.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | set current_capacity to do shell script "/usr/sbin/ioreg -w0 -l | grep -E 'CurrentCapacity' |sed 's/.*= \\(.*$\\)/\\1/'" set max_capacity to do shell script "/usr/sbin/ioreg -w0 -l | egrep -wE 'MaxCapacity' |sed 's/.*= \\(.*$\\)/\\1/'" set charge_percentage to round (current_capacity / max_capacity * 100) if charge_percentage ≥ 99 then tell application "GrowlHelperApp" set the allNotificationsList to ¬ {"BatteryChargeReminder", "BatteryChargeReminder"} set the enabledNotificationsList to ¬ {"BatteryChargeReminder"} register as application ¬ "BatteryChargeReminder" all notifications allNotificationsList ¬ default notifications enabledNotificationsList ¬ icon of application "Activity Monitor" end tell tell application "GrowlHelperApp" notify with name ¬ "BatteryChargeReminder" title ¬ "BatteryChargeReminder" description ¬ "Time to unplug! Percentage Remaining: " & charge_percentage & "%" application name "BatteryChargeReminder" end tell end if |
Then using cron, I schedule a job to call this script every 10 minutes
1 2 | 90kts:~ koops$ crontab -e */10 * * * * osascript ~/Library/Scripts/BatteryChargeReminder.scpt |
That’s it! Now you have a nice (annoying) reminder to tell you to unplug if need be …









5 Responses to “A Growl Hack to Remind You to Unplug Your Charger”
I know you are a hard core Unix Wannabe, so cron was your first thought - but since this is a Mac Post I thought I would let you know that cron was deprecated for launchd from 10.4 onwards. See http://developer.apple.com/macosx/launchd.html
By Lantrix on May 6, 2008
So do I really want to type all this:
Just to replace 1 line in cron?
Nah. Deprecated, but still supported …
By Tim on May 6, 2008
Using MacOS 10.5.2 this script failed for me. the awks you did on ioreg came back with the pipe character. Also the MaxCapacity grep matched 3 lines.
Instead of being reliant on the result being on a specific column, I used sed to look for the value after the equal sign. I also changed the grep on the MaxCapacity line to egrep and match a whole word, thereby eliminating the other matches (AbsoluteMaxCapacity). Here are the first two lines changed to suit:
You will see I had to escape the sed backslashes for the applescript.
By Lantrix on May 6, 2008
Yeah OK I used cron too. Could not be stuffed finding where to put the plist.
By Lantrix on May 6, 2008
No probs, thanks for the improvements. Probably because u are on different hardware to me. Original script works fine on macbookpro, but have since changed to ur more resilient masterpiece …
cheers
Tim
By Tim on May 6, 2008