Subscribed unsubscribe Subscribe Subscribe

電池の残りを通知する

use strict;
use warnings;

use AnyEvent;
use AnyEvent::WebService::ImKayac;
use Config::Pit;

my $charging = `ioreg -l | grep "IsCharging"`;
my $capacity = `ioreg -l | grep "Capacity"`;

my ($max_capacity) = $capacity =~ /"MaxCapacity" = (\d+)/;
my ($current_capacity) = $capacity =~ /"CurrentCapacity" = (\d+)/;
my $is_charging = $charging =~ /Yes/;

exit 0 if $is_charging;

my $rate = ($current_capacity / $max_capacity) * 100;

if ( $rate < 20 ) {
    my $cv = AE::cv;
    my $imkayac = AnyEvent::WebService::ImKayac->new(%{ pit_get("im.kayac.com") });
    $imkayac->send( message => sprintf("remain battery capacity rate: %.2f%%", $rate), cb => sub { $cv->send; });
    $cv->recv;
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>hogehoge.hogehoge.hogehoge</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/zsh</string>
        <string>-c</string>
        <string>exec perl /path/to/battery_imkayac.pl</string>
    </array>
    <key>StartInterval</key>
    <integer>1800</integer>
</dict>
</plist>

適当にこんな感じで、launchctl loadしておくと、電源につないでない時にmacbookairの電池が残り20%を切ると、imkayacに通知してくれるので、酔っ払った後に放置しても0%になる前に気づくかもしれないです。