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:

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:

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’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:
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.
1 Comment