Finder AppleScript: Eject Volume and Trash its Disk Image
Mac OS X — 29 Oct 2008 22:41 — 1199 days ago

I wrote the AppleScript below to eject one or more selected volumes in the Finder. If a volume represents a disk image, the script trashes the image at the same time. I use this a lot with downloaded application distribution images.

I run the script with FastScripts with the keyboard short cut Cmd-Option-E. Unfortunately, there seems to be a Finder bug that makes the script take a very long time do finish.

set myVolumes to getSelectedVolumes()
if (count myVolumes) < 1 then return

set myImages to getImageList()

repeat with myVolume in myVolumes
	set myImagePath to ""
	set myImageFile to null
	repeat with myImage in myImages
		repeat with myEntity in |system-entities| of myImage
			try
				set mountPoint to |mount-point| of myEntity
				displayed name of myVolume
				if mountPoint = ("/Volumes/" & displayed name of myVolume) then
					set myImagePath to |image-path| of myImage
					set myImageFile to POSIX file myImagePath
				end if
			end try
		end repeat
	end repeat
	
	tell application "Finder"
		eject myVolume
		if myImageFile is not null then
			try
				delete file myImageFile
			end try
		end if
	end tell
end repeat



on getSelectedVolumes()
	set volumeList to {}
	tell application "Finder"
		set mySelection to selection
		repeat with myItem in mySelection
			set myItem to contents of myItem
			--			log class of myItem
			if kind of myItem is "Volume" then
				set end of volumeList to myItem
			end if
		end repeat
	end tell
	return volumeList
end getSelectedVolumes



on getImageList()
	set diskinfo to do shell script "hdiutil info -plist"
	tell application "System Events"
		set diskinfo to value of (make new property list item with properties {text:diskinfo})
		set imageList to |images| of diskinfo
	end tell
	return imageList
end getImageList

Comments
Posted by Nick Ludlam on 30 Oct 2008 13:02

Which section of the script hangs? If it's the delete statement, could you circumvent the Finder and "mv <file> ~/.Trash/" ?

Posted by Marc on 30 Oct 2008 14:11

Ejecting volumes seems to be the problem...

Powered By blojsom