How do I run an Applescript automatically when my iPod mounts?

I would like to run a script that copies specified text files into my iPod’s Note Folder automatically when my iPod mounts. I have the script written & it works when my iPod is connected and I manually run it from Script Editor.

However, I know next to nothing about getting this script to execute automatically. Can someone point me in the right direction?

Thanks,
C.W.

Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.3) Gecko/20060427 Camino/1.0.1
Operating System: Mac OS X (10.4)

I don’t have an iPod, so can’t be specific, but how does the iPod “present itself” when you plug it in? Does it show up as a volume?

When you plug it in, does it show up when you run this?

tell application "Finder" to get name of every disk

If the answer to that is “yes”, then a stay-open idle handler is the way to go.

on idle
	tell application "Finder" to set N to name of disks
	if myIpod_Name is in N then
		-- do your thing
	end if
	return 30 -- this is in seconds
end idle

maybe a folder action on the /Volumes folder
to get to it open terminal and type
open /Volumes
and attach a folder action to it on adding item then check if the item is you ipods name

Yeah, checking the name with a folder action is probably easiest, like so:

property iPodNames : {"My iPod"}

on adding folder items to thisFolder after receiving theseItems
	repeat with thisDisk in theseItems
		set thisDisk to thisDisk as alias
		set thisInfo to info for thisDisk without size
		set thisName to name of thisInfo
		
		if thisName is in iPodNames then
			set notesPath to thisName & ":Notes" as alias
			
			-- Copy files to Notes folder
			
		end if
	end repeat
end adding folder items to

Or you can check if the folder iPod_Control exists on the disk, something which all iPods should have:

on adding folder items to thisFolder after receiving theseItems
	repeat with thisDisk in theseItems
		set thisDisk to thisDisk as Unicode text
		tell application "System Events" to set isIPod to folder (thisDisk & "iPod_Control") exists
		
		if isIPod then
			set notesPath to thisDisk & "Notes" as alias
			
			-- Copy files to Notes folder
			
		end if
	end repeat
end adding folder items to

Or you can use the tool diskutil to do your checking:

on adding folder items to thisFolder after receiving theseItems
	repeat with thisDisk in theseItems
		set thisDisk to text 1 thru -2 of (thisDisk as Unicode text)
		set isIPod to (do shell script "diskutil info `diskutil list " & "/Volumes/" & quoted form of thisDisk & " | head -n 1` | awk '/Media Type/ {match($0,/:[[:space:]]*/); print substr($0,RSTART+RLENGTH,length)}'") is "iPod"
		
		if isIPod then
			set notesPath to thisDisk & ":Notes" as alias
			
			-- Copy files to Notes folder
			
		end if
	end repeat
end adding folder items to

I would probably go with the first, since the last two will work for any iPod connected, while the first will only execute if it has a certain name.

This is definitely the way to go.

If you just type “/Volumes” in the AppleScript Folder Actions Setup utility, and choose the script that you want to run on the right, it will work - the Folder Action will be triggered by anything added to the /Volumes directory, and that happens when any volume including the iPod is mounted.

The folder action would be roughly
on adding folder items to theFolder after receiving theItems
if “MyiPod” is in theItems then set iPodIsMounted to true
else set iPodIsMounted to false
end if
end adding folder items to

Thanks for all the ideas.

I am trying to use this Folder Action attached to /Volumes

on adding folder items to this_folder after receiving the_items
	
	display dialog "running folder action" --this is for debugging so I will know if the script is running
	
	set myIpod_Name to "My Ipod" --except it really is my iPod's name
	
	if myIpod_Name is in the_items then
		set myScript to ((path to scripts folder from user domain as text) & "transferNotes.scpt") as alias
		run script myScript
	end if
	
end adding folder items to

This is what I did:

  1. in terminal, typed “open /Volumes”
  2. ctl-clicked the “Volumes” & did “Attach a Folder Action…”
  3. browsed to & selected the script file, “transferNotesFolderAction.scpt” (what’s in the box above). It is located in ~/Library/Scripts
  4. plugged in my iPod
  5. nothing happened
    I can see where there might be errors in script execution (like I mistyped my iPod’s name or something), but I should at least see the dialog box, right?

Any thoughts?

Check the /Volumes directory before and after plugging in the iPod to make sure that part it working.

cd /Volumes
ls -la

or in AppleScript,

tell app “Finder” to list disks

Thanks for all your help. I got it working. Here’s my final solution:

property iPodNames : {"My iPod"}

on adding folder items to this_folder after receiving the_items
	
	repeat with thisDisk in the_items
		if (name of (info for (thisDisk as alias) without size)) is in iPodNames then
			
			set myScript to ((path to scripts folder from user domain as text) & "transferNotes.scpt") as alias
			run script myScript
			
		end if
	end repeat
	
end adding folder items to