Delete dmg disk image automatically on eject on mac osx

This post will show you how to delete dmg disk image automatically – it will delete a mac disk image file (dmg) when you eject it from your mac

There is an easier way to do the following (just drag the mounted dmg file to the trash, but this is a nice solution too)

I found the instructions on lifehacker but for redundancy I’ve copied the instructions here

(it also makes it easier for me to find them later!)

Here are the instructions:

Open up the Automator application and select “Service”.

Change the top two dropdown menus to receive “No Input” from the “Finder”.

In the left sidebar, drag the “Run AppleScript” action into the right pane. Copy the following code into the box:


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

 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

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

 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 '/.*'")
 set match_found to false
 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

 if match_found is not equal to true then
   display dialog ¬
   "The selected volume does not appear to be a Disk Image." 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

Save the service as whatever you want (like “Eject & Delete DMG”) and exit Automator.

Leave a Reply