Open a dialog whenever a user loads a zip disk into the zip drive

hello all,

i am a recent switcher (yay) that has a quick question about applescript. i need to put together a script that will pop open a dialog whenever a user loads a zip disk into the zip drive. i would assume that what i have to do is save the script as an application and have the application run at login. and though i can see various examples of how to have applescript interact with a particular application, i haven’t run across anything yet that would help me use applescript to monitor a device. i basically need something that will occur when something is mounted. is this even possible? and if so, could anyone point me to a good resource to see how such a thing is accomplished?

thanks

just a thought, but i might not have to be able to interact with a device. presumably, the drive sends a message to the finder when something is mounted. perhaps i could query the finder application. any thoughts?

The easiest way to do this is to use a stay open application that checks for new disks every x amount of time (I’d say 10 seconds or so to limit CPU cycle waste) and yes, add it to your login items. So, save this code as a stay open app from Script Editor:

property last_disk_check : {}

on run
	set last_disk_check to list disks
end run

on idle
	set current_disk_check to list disks
	if current_disk_check is not last_disk_check then
		if (count current_disk_check) > (count last_disk_check) then
			set the_action to "mounted"
			set new_disk to my find_difference(current_disk_check, last_disk_check)
		else
			set the_action to "ejected"
			set new_disk to my find_difference(last_disk_check, current_disk_check)
		end if
		tell application "Finder"
			activate
			set the_button to button returned of (display dialog "The disk " & new_disk & " was " & the_action & ". Should I continue checking for disk mount activity?" buttons {"No", "Yes"} default button 2 with icon 1 giving up after 10)
		end tell
		if the_button = "No" then quit
		set last_disk_check to current_disk_check
		if the_action = "mounted" then
			--do something 
		else
			--do something else
		end if
	end if
	return 10 --seconds
end idle

on find_difference(list_a, list_b)
	repeat with this_item in list_a
		set this_item to contents of this_item
		if this_item is not in list_b then return this_item
	end repeat
end find_difference

Note that you can then do something else depending on whether the disk was mounted or ejected. This will only test for one disk but if you had mounted several disks (network volumes count as well as ejectable floppies, Zips, & other disks) at once (or unmounted them), this will only report on the first difference is noted though this could easily be rectified.

There may be other solutions out there for triggering a script when a disk is mounted/ejected but I’m not sure what they are and they will probably add more potential conflicts to your system than this solution.

Jon