Daniel Bergey Commotion

Seriously, go plug in your computer.

This happens a lot: I’ll be programming along, in the zone, so much that even the low-power alert doesn’t dissuade me from my task:

reservepower.png

That’s bad. Because inevitably, I’ll be just in the middle of a thought when the computer shuts off, ten minutes later. I don’t actually lose anything, of course, because MacBooks are usually smart about keeping enough power to stay alive. (Note: haven’t used anything else for a while; other platforms may do just as well. YMMV.)

Still. It happened so much that I decided to do something about it:

seriouslyplugin.png

I made a new text file, named it "batterywarning" and threw it in /Applications/Scripts/ with the following contents:

#!/usr/bin/env ruby

`pmset -g batt` =~ /discharging; (\d+:(\d+)) remaining/;
if $1 && $1 < '0:10' then
  nums = %w{ zero one two three four five six seven eight nine ten }
  mins = nums[$2.to_i] ? (nums[$2.to_i] +" minute"+ ($2=='01' ? '' : 's')) : $1
  exec "osascript -e 'tell application \"AppleScript Runner\" to "+
    "display alert \"Seriously, go plug in your computer.\" "+
    "message \"There&rsquo;s only about "+ mins + " left! "+
    "How many times do you need to be told?\" as warning'"
end

The gist: when run, the script checks the current estimated remaining time on the battery. If it’s below 10 minutes (the point at which the native warning appears), it displays its own alert box warning the user of their procrastinatory folly. If there is more than 10 minutes remaining, it does nothing.

I made the script executable (chmod +x /path/to/script), and configured launchd to run it every minute by creating a plist named "com.dbergey.batterywarning.plist" in ~/Library/LaunchAgents/ thusly:

<?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>com.dbergey.batterywarning</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/Scripts/batterywarning</string>
    </array>
    <key>LowPriorityIO</key>
    <true/>
    <key>StartInterval</key>
    <integer>60</integer>
</dict>
</plist>

Then, from the command line, I did this to tell launchd about it:

launchctl load ~/Library/LaunchAgents/com.dbergey.batterywarning.plist 

And it worked! Hopefully I will be more proactive about plugging in when warned up to 10 times before shutoff. :)


2 Comments

That’s kind of awesome (you know it really is and i’m just saying it like that cause i feel like it ;) )! And it’s so frustrating to have one’s computer turn off in the middle of doing something, so it’s time someone did something about the procrastinating (i totally spelled that wrong, but oh well) of America’s computer users who don’t plug in there computers!!!!! Myself being one of them….. :D

Posted by Sarah on 6 November 2009 @ 6pm

Maybe you should have that wired into your dev build scripts so projects simply refuse to build until you plug in.

Posted by Hans on 29 June 2010 @ 1pm

Leave a Comment