How to run a script to do useful stuff when you plug-in your iPod

<span class="comment">-- This is a Folder Actions AppleScript that triggers an
-- event when a disk (like an iPod) is mounted by the
-- System. I use it to run a compiled Automator workflow that
-- backs-up my Keychains, and copies my GTD lists to the
-- iPod notes folder.
-- 
-- To run it you should tweak the script accordingly, and add it
-- as a Folder Action to the "/Volumes" folder. It isn't
-- exactly obvious how to do this, so here's how:
-- 
-- 1\. Open the "Folder Actions Setup" app.
-- 2\. Click the '+' button to add a new Folder Action.
-- 3\. The "/Volumes" directory is hidden from view. Don't click
--    anything, type "/Volumes" to get to it.
-- 4\. Add this script as a Folder Action.</span>

<span class="language-keywords">on</span> <span class="application-keywords">adding folder items to</span> <var>thisFolder</var> <span class="application-keywords">after receiving</span> <var>addedItems</var>
    <span class="language-keywords">set</span> <var>volumeName</var> <span class="language-keywords">to</span><span> "Tim Brayshaw's iPod"</span> <span class="comment">-- change this!</span>
    <span class="language-keywords">repeat</span> <span class="language-keywords">with</span> <var>addedItem</var> <span class="language-keywords">in</span> <var>addedItems</var>
        <span class="language-keywords">if</span> <span class="language-keywords">the</span> <span class="application-keywords">displayed name</span> <span class="language-keywords">of</span> <span class="operator">(</span><span class="application-keywords">info for</span> <var>addedItem</var><span class="operator">)</span> &not;
            <span class="language-keywords">is</span> <span class="language-keywords">equal to</span> <var>volumeName</var> <span class="language-keywords">then</span>
            <var>volumeMounted</var><span class="operator">()</span>
            <span class="language-keywords">exit</span> <span class="language-keywords">repeat</span>
        <span class="language-keywords">end</span> <span class="language-keywords">if</span>
    <span class="language-keywords">end</span> <span class="language-keywords">repeat</span>
    <span class="comment">-- System Events seems to keep file references open to each of
    -- the "addedItems" above, this means you can't eject these
    -- volumes. This bodge quits System Events, so you can eject
    -- the added volumes. (System Events gets launched immediately
    -- again.)</span>
    <span class="application-keywords">quit application</span> "System Events"
<span class="language-keywords">end</span> <span class="application-keywords">adding folder items to</span>

<span class="language-keywords">on</span> <var>volumeMounted</var><span class="operator">()</span>
    <span class="language-keywords">tell</span> <span class="application-keywords">application</span><span> "Finder"</span>
        <span class="application-keywords">open</span> <span class="application-keywords">application</span><span> "Update iPod"</span>
    <span class="language-keywords">end</span> <span class="language-keywords">tell</span>
<span class="language-keywords">end</span> <var>volumeMounted</var>

This should world for other devices like USB thumbdrives, external FireWire disks, Digital Cameras etc. I futzed around trying to figure out how to do this with launchd, but gave up. If anyone knows a better or other way, post a comment!

I’ve found this useful because it’s removed some backup anxieties, and means I’ve always got my shopping and out-and-about errands lists (and so on) with me when I’m away for the computer. There’s probably a better way of doing this, but it took me an inordinate amount of time to figure it out, so I thought I’d post a code snippet here for like-minded dabblers.

Comments