Trash dmg Files When Ejecting Disk Images

We’re going to start off locating the selected disk in the Finder.

tell application "Finder"
	set selection_list to selection
	if (count selection_list) < 1 then
		display dialog ¬
			"Please select a volume mounted from a disk image." with title ¬
			"No Selection Found" with icon stop ¬
			buttons ["OK"] default button 1
		return
	end if

The above snippet looks for a selection, and if it doesn’t find one, displays an error message. Return is a reserved word in scripting languages that means “I’ve completed this section of code. Here is the result.” Since there is only one section (our main script), and we don’t need to return any data, we don’t need any value to follow after ‘return’. If we did, an example might be return “my value” or return my_variable.

	set my_selection to item 1 of selection_list
	set my_kind to kind of my_selection
	set my_name to name of my_selection

Here we define some of our variables. Since a selection can contain multiple items, we’re just looking at the first one.  A good exercise for the reader would be to use a loop to repeat our script for all selected items.

	if my_kind is not "Volume" then
		display dialog ¬
			"Please select a volume mounted from a disk image." with title ¬
			"Selection is not a Disk Image" with icon stop ¬
			buttons ["OK"] default button 1
		return
	end if

If the selected item isn’t a Volume, we need to alert the user that our script won’t work, and then stop the script.

	set volume_list to paragraphs of (do shell script "hdiutil info | grep ^/dev/disk | grep -o '/Volumes/.*'")
	set source_list to paragraphs of (do shell script "hdiutil info | grep ^image'-'alias | grep  -o '/.*'")

AppleScript’s “do shell script” command allows us to use unix commands.  Just enclose the command you would run in the terminal in quotes.

The command hdiutil allows us to work with disk images from the command line.  It’s a great utility for mounting, unmounting, examining, and creating disk images.  For more information you can type “man hdiutil” into the terminal, or read the manual page online.

Both commands are actually three commands connected together with the | character.  “hdiutil info” outputs the information for each mounted disk image.  “grep ^/dev/disk” looks for lines that start with “/dev/disk”.  “grep -o ‘/Volumes/.*'” outputs the text from the previous grep starting with ‘/Volumes/’ (if the lines contain that text). The grep command uses regular expressions to search through text.  Here is a reference document of regular expression special characters if you’re interested in learning more.

Similar to the first command, the second command outputs the disk image’s information, searches for lines that start with “image-alias”, and then outputs the line’s text after the first ‘/’ character.

In AppleScript, we take the output, splice the text by every line (or paragraph as AppleScript calls it), and we create a list (each item will be one line of text).

	repeat with v from 1 to (count volume_list)
		if "/Volumes/" & my_name = item v of volume_list then
			set match_found to true
			exit repeat
		end if
	end repeat

Now we go search through our list of volumes to look for the volume that will match our selection.  Since our volume list contains unix formated paths, we need to convert our disk image name to the same path format.

	if match_found is not equal to true then
		display dialog ¬
			"The selected volume could not be found in the Volume list." with title ¬
			"Could not find Disk Image" with icon stop ¬
			buttons ["OK"] default button 1
		return
	else
		set my_source to POSIX file (item v of source_list) as alias
		move my_source to the trash
		eject my_selection
		--reveal my_source
	end if
end tell

If the script found a match, it locates the matching item in the list of dmg files; otherwise, it returns an error message.  Now that we’ve found the disk image file, we can manipulate the file however we like using AppleScript.  In this example, I’m moving the image file (my_source) to the trash, and ejecting the mounted disk (my_selection).  You could also simply show the disk image in the Finder using the “reveal my_source” command (which I’ve commented out in the above example).

I’m providing a complete copy of this script that can be opened in the Script Editor application or invoked from a number of helper applications.  You could use it with the Script Menu, Download the script Trash_and_Eject.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.