A Growl Hack to Remind You to Unplug Your Charger

If 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%.
Battery Charge Growl Notification Trans

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 …

6 comments 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

  • Tim

    So do I really want to type all this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
    "http://www.apple.
    com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
    <key>Label</key>
    <string>com.apple.periodic-daily</string>
    <key>ProgramArguments</key>
    <array>
    <string>/usr/sbin/periodic</string>
    <string>daily</string>
    </array>
    <key>LowPriorityIO</key>
    <true/>
    <key>Nice</key>
    <integer>1</integer>
    <key>StartCalendarInterval</key>
    <dict>
    <key>Hour</key>
    <integer>3</integer>
    <key>Minute</key>
    <integer>15</integer>
    </dict>
    </dict>
    </plist>

    Just to replace 1 line in cron?
    Nah. Deprecated, but still supported …

  • 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:

    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/'"

    You will see I had to escape the sed backslashes for the applescript.

  • Yeah OK I used cron too. Could not be stuffed finding where to put the plist.

  • Tim

    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

  • Hi..
    Thank you for sharing such a nice information for scripting work of charger. I will surely bookmark it for future use. Good Work! Keep it up! This Scripting language is very useful for me. I would like to appreciate you.

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">