<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	 xmlns:slash="http://purl.org/rss/1.0/modules/slash/" 
>

<channel>
	<title>Daniel Bergey &#187; Code</title>
	<atom:link href="http://danielbergey.com/motion/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://danielbergey.com/motion</link>
	<description>Commotion</description>
	<lastBuildDate>Mon, 02 Nov 2009 02:45:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Seriously, go plug in your computer.</title>
		<link>http://danielbergey.com/motion/2009/11/seriously-go-plug-in-your-computer/</link>
		<comments>http://danielbergey.com/motion/2009/11/seriously-go-plug-in-your-computer/#comments</comments>
		<pubDate>Sun, 01 Nov 2009 13:24:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[launchd]]></category>
		<category><![CDATA[macbook]]></category>
		<category><![CDATA[power]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://danielbergey.com/motion/?p=662</guid>
		<description><![CDATA[This happens a lot: I&#8217;ll be programming along, in the zone, so much that even the low-power alert doesn&#8217;t dissuade me from my task: That&#8217;s bad. Because inevitably, I&#8217;ll be just in the middle of a thought when the computer shuts off, ten minutes later. I don&#8217;t actually lose anything, of course, because MacBooks are [...]]]></description>
			<content:encoded><![CDATA[<p>This happens a lot: I&#8217;ll be programming along, in the zone, so much that even the low-power alert doesn&#8217;t dissuade me from my task:</p>

<div align="center" style="margin: -25px -45px;"><img src="http://danielbergey.com/motion/wp-content/uploads/2009/11/reservepower.png" alt="reservepower.png" border="0" width="555" height="243" style="border-width: 0;" /></div>

<p>That&#8217;s bad. Because inevitably, I&#8217;ll be just in the middle of a thought when the computer shuts off, ten minutes later. I don&#8217;t actually <em>lose</em> anything, of course, because MacBooks are usually smart about keeping enough power to stay alive. <em>(Note: haven&#8217;t used anything else for a while; other platforms may do just as well. <acronym title="Your Mileage May Vary">YMMV</a>.)</em></p>

<p>Still. It happened so much that I decided to do something about it:</p>

<div align="center" style="margin: -25px -45px;"><img src="http://danielbergey.com/motion/wp-content/uploads/2009/11/seriouslyplugin.png" alt="seriouslyplugin.png" border="0" width="500" height="233" style="border-width: 0;" /></div>

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

<pre><code>#!/usr/bin/env ruby

`pmset -g batt` =~ /discharging; (\d+:(\d+)) remaining/;
if $1 &amp;&amp; $1 &lt; '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&amp;rsquo;s only about "+ mins + " left! "+
    "How many times do you need to be told?\" as warning'"
end
</code></pre>

<p>The gist: when run, the script checks the current estimated remaining time on the battery. If it&#8217;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.</p>

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

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
&lt;plist version="1.0"&gt;
&lt;dict&gt;
    &lt;key&gt;Label&lt;/key&gt;
    &lt;string&gt;com.dbergey.batterywarning&lt;/string&gt;
    &lt;key&gt;ProgramArguments&lt;/key&gt;
    &lt;array&gt;
        &lt;string&gt;/Applications/Scripts/batterywarning&lt;/string&gt;
    &lt;/array&gt;
    &lt;key&gt;LowPriorityIO&lt;/key&gt;
    &lt;true/&gt;
    &lt;key&gt;StartInterval&lt;/key&gt;
    &lt;integer&gt;60&lt;/integer&gt;
&lt;/dict&gt;
&lt;/plist&gt;
</code></pre>

<p>Then, from the command line, I did this to tell <code>launchd</code> about it:</p>

<pre><code>launchctl load ~/Library/LaunchAgents/com.dbergey.batterywarning.plist 
</code></pre>

<p>And it worked! Hopefully I will be more proactive about plugging in when warned up to 10 times before shutoff. <img src='http://danielbergey.com/motion/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="smartshare" style="display: none;"></div>]]></content:encoded>
			<wfw:commentRss>http://danielbergey.com/motion/2009/11/seriously-go-plug-in-your-computer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	<slash:comments>2</slash:comments>
	</item>
	</channel>
</rss>
