Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Give your battery a thorough checkup' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Give your battery a thorough checkup
Authored by: kb5ql on Mar 13, '03 06:50:04PM

This hint is great in that I was able to take the command, and parse the relevant info in perl, then pop up an applescript alert. This is great for giving yourself a warning in phases instead of waiting for the battery indicator to go red.

Now you may say, why, since the icon on the menubar already does this? I guess I hate having the battery icon take up half the menubar with the remaining percentage on it.

copy the following to a plain text file save as .battery_check.pl in your home dir (or wherever)

---START SCRIPT--- copy after this
#!/usr/bin/perl -w

use strict;

my $threshold = $ARGV[0];

#if no value passed to script use default check value of 15% or less
#before we start with the whiny applescript warnings

if (!$threshold) { $threshold = 15; }

my $osascript_name = "/tmp/battery_check.scpt";
my (%plist,$msg);
my @cmd_output = `/usr/sbin/ioreg -p IODeviceTree -n "battery" -w 0`;

#looking for the following string:

#"IOBatteryInfo" = ({"Voltage"=11925,"Flags"=4,"Amperage"=1705,"Capacity"=4149,"Current"=3717})

foreach my $line (@cmd_output)
{
if ($line =~ /IOBatteryInfo/i) { %plist = &formatBatteryInfo($line); }
}

#foreach (keys %plist) { print "$_ => $plist{$_} \n"; }

#my $voltage = sprintf("%.2f", ($plist{'Voltage'}/1000));
#this calculates the remaining charge left...
my $percentage = sprintf("%.1f", ($plist{'Current'}/$plist{'Capacity'})*100);
$msg = "\"Voltage : $plist{'Voltage'} Amperage: $plist{'Amperage'}\"" . '& return &';
$msg .= "\"Capacity : $plist{'Capacity'} Current: $plist{'Current'}\"" . '& return & return &';
$msg .= "\"Remaining Charge : [$percentage%] \"" . '& return & return';

if ($percentage < $threshold)
{
(my $osascript = <<"eoj") =~ s/^\t+//gm;
tell application "Finder"
set myAlert to $msg
display dialog myAlert buttons {"OK"} with icon caution
end tell
eoj

open(OUTFILE,">$osascript_name");
print OUTFILE $osascript;
close(OUTFILE);

system("osascript $osascript_name");
}
#### SUBS #####

sub formatBatteryInfo
{
my %plist;
my $info = shift;

$info =~ s/^.+"IOBatteryInfo"([^\{]+)\{([^\}]+)\}.+/$2/i;
#sample result from this regex:
#"Voltage"=11572,"Flags"=4,"Amperage"=1894,"Capacity"=4149,"Current"=3298
my @name_values = split(/,/,$info);
foreach my $pair (@name_values)
{
if ($pair =~ /"([^"]+)"\s*=\s*(.+)/) { $plist{$1} = $2; }
}

foreach (keys %plist) { $plist{$_} = sprintf("%.2f", ($plist{$_}/1000)); }

return %plist;
}

--END SCRIPT -- don't copy this line

you invoke this script by running it from the command line:

perl /Users/<yourname>/.battery_check.pl 15

This will only show an alert if you have 15% or less remaining charge.

After you have done this, you can set up a cron job to run this whenever you like. Thanks for the command tip for getting this info!

Here are my crontab entries for the script:

*/30 * * * * perl /Users/mjelks/.battery_check_alert 40 > /tmp/batt.log 2>&1
*/5 * * * * perl /Users/mjelks/.battery_check_alert 8 > /tmp/batt.log 2>&1

it will check every 30 mins for a charge of 40% or less , the second line will check for a charge of 8% or less every 5 mins



[ Reply to This | # ]