Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 05-11-2009, 08:41 AM   #1
rak
Prospect
 
Join Date: May 2009
Posts: 4
Question Triggering Applescript on Low Battery Warning

Is it possible to trigger an Applescript when the battery reaches a low (~20%) level? or when the low battery warning pops up?

Could you have a script running in the background that looks at the output of

Code:
ioreg -wO -l | grep Capacity
and trigger Sleep?
rak is offline   Reply With Quote
Old 05-11-2009, 01:10 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,293
Yes you could.
__________________
hayne.net/macosx.html
hayne is offline   Reply With Quote
Old 05-11-2009, 01:41 PM   #3
rak
Prospect
 
Join Date: May 2009
Posts: 4
Thumbs up

Haynes, thanks for confirming my suspicions!

I found this old perl script which checks the battery level and calls an Applescript:
http://www.zaftig.net/node/322 (code listing )

But it's doesn't work in Leopard, the options for ioreg and the output format are different:
Code:
/usr/sbin/ioreg -p IODeviceTree -n "battery" -w
Output:
Code:
"IOBatteryInfo" = ({"Voltage"=11925,"Flags"=4,"Amperage"=1705,"Capacity"=4149,"Current"=3717})
I tried modifying it with the Leopard-friendly command I mentioned originally and replacing references to IOBatteryInfo with LegacyBatteryInfo
Code:
ioreg -wO -l | grep Capacity
Output:
Code:
"LegacyBatteryInfo" = {"Capacity"=4149,"Voltage"=11925,"Flags"=4,"Current"=3717,"Amperage"=1705,"Cycle Count"=123}
As the sequence of the output is different it fails, the regex in the "formatBatteryInfo" section of the code (line 65 onwards) needs to be modified. I'm not fluent in regex, so have no idea how to extract the Capacity and Current values.

Any help would be appreciated!
rak is offline   Reply With Quote
Old 05-11-2009, 02:16 PM   #4
NovaScotian
League Commissioner
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 5,073
Something like this will extract the current percentage:

set Cap to (do shell script "ioreg -wO -l | grep Capacity")
tell Cap to set {Available, Max} to {last word of paragraph 1, last word of paragraph 4}
set Pct to round (100 * Available / Max)
__________________
17" MBP, OS X; 27" iMac, both OS X 10.9.2
NovaScotian is offline   Reply With Quote
Old 05-11-2009, 07:12 PM   #5
rak
Prospect
 
Join Date: May 2009
Posts: 4
Thanks, NovaScotian. That's a lot more simple!
rak is offline   Reply With Quote
Reply

Tags
applescript, battery

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 04:39 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.