Skip to content

Instantly share code, notes, and snippets.

@markpasc
Created January 18, 2010 18:53
Show Gist options
  • Save markpasc/280256 to your computer and use it in GitHub Desktop.
Save markpasc/280256 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
# Show battery status from LaunchBar.
# Drop this in your App Support/LaunchBar/Actions dir, chmod it +x, and have
# LaunchBar reindex to find it.
package Battery;
use Encode;
use charnames ':full';
sub batteriness {
my $class = shift;
my $what = `ioreg -r -c AppleSmartBattery`;
$what =~ s= \A [^{]* ==xmsg;
$what =~ s/ (?<=}) [}]* //xmsg;
my %data;
for my $datum (split /\n/, $what) {
$datum =~ m/ \A \s* "([^"]+)" \s*=\s* ([^\n]+) /xmsg;
$data{$1} = $2;
}
my $is_charging = $data{IsCharging} =~ m{No}xmsg ? undef : 1;
my $capacity = $data{CurrentCapacity};
my $max_cap = $data{MaxCapacity};
return {
is_charging => $is_charging,
capacity => 0 + $capacity,
max_capacity => 0 + $max_cap,
};
}
sub main {
my $class = shift;
# Seems like we can't script back at LaunchBar to display while it's
# waiting on us, so fork and don't wait.
return if fork;
my $data = $class->batteriness();
my $batt = $data->{is_charging} ? encode('utf-8', "\N{HIGH VOLTAGE SIGN} ") : " ";
my $charge = $data->{capacity} / $data->{max_capacity};
$charge = int ($charge * 6);
#print "($charge) ";
my ($dark, $light) = (encode('utf-8', "\N{DARK SHADE}"), encode('utf-8', "\N{LIGHT SHADE}"));
$batt .= (($dark) x $charge);
$batt .= (($light) x (6 - $charge));
system qq{osascript -e 'tell app "LaunchBar" to display in large type "$batt"'};
}
__PACKAGE__->main() unless caller;
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment