Tim’s
Bandwagon

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


-- 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.

on adding folder items to thisFolder after receiving addedItems
    set volumeName to "Tim Brayshaw's iPod" -- change this!
    repeat with addedItem in addedItems
        if the displayed name of (info for addedItem) ¬
            is equal to volumeName then
            volumeMounted()
            exit repeat
        end if
    end repeat
    -- 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.)
    quit application "System Events"
end adding folder items to

on volumeMounted()
    tell application "Finder"
        open application "Update iPod"
    end tell
end volumeMounted

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.

Leave a Comment

Your personal information

Your comment