Need help with running a script upon mounting a volume

I just got a new iPod, which I’m using as an external hard drive for storing my personal movie files, for use in iMovie and iDVD projects (since my PB’s HD was running low on space). To maintain integration between my iApps (iMovie, iDVD, etc.), I’d like to write a script that, upon mounting of my iPod, will rename my existing /user/Movies directory to something else (like Movies_orig), getting that out of the way, and create an alias named Movies in my user folder to take the place of the original Movies folder, which points to the Movies folder on my iPod. Of course, I’d also like the script to undo these changes upon unmounting of my iPod. The copying/renamine/alias-making, I can probably handle (although I could use help making it clean and foolproof), but I don’t know how to create a script that will run upon mounting of a specific volume.
Can one (or more) of you expert AS’ers help me out?

Take a look at

This may give you a good start !


Using the Folder Actions Setup app you can link this script to the /Volumes directory:

on adding folder items to thisFldr after receiving theseVolumes
	repeat with itm in theseVolumes
		tell application "Finder" to set n to name of itm
        if n = "name of my iPod" then
            -- do iPodish stuff
        end if
	end repeat
end adding folder items to

and similarly the “on removing folder items from thisFldr after losing theseItems” handler can be used to handle the dismount stuff.

  • Dan

Awesome, guys, thanks. I think I’ll use the “Do Something With” prefpane Paul linked to, as it’s a bit simpler for my purposes. Now I need help with creating the alias to the iPod Movies folder. Here’s what I have now, which isn’t working:

[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

The shell script successfully renames the existing ~/Movies folder, but the finder tell statement isn’t creating a folder alias to the iPod’s Movies folder. And there are no useful output or error messages in Script Editor when I run it. What am I doing wrong?
Thanks again!

Aha! I’ve accomplished what I wanted to do! On mounting, I simply run this script:

And upon unmounting, I run this script:

God bless the “do shell script” command! How silly of me to overlook such a simple solution.
BTW, if anyone sees any potential for problems with this approach, please let me know!

I wasn’t able to see the /Volumes directory in Folder Actions Setup’s file dialog. I had to programmatically specify the /Volumes folder with

set theVolumesFolderAlias to ((path to startup disk as text) & "Volumes:") as alias

followed by

set theAttachedScriptAlias to (choose file with prompt "Choose script to attach to /Volumes." of type ¬
	"osas" default location path to Folder Action scripts folder without invisibles)
tell application "System Events" to attach action to theVolumesFolderAlias using theAttachedScriptAlias