Automatically Delete A DMG When You Eject It In OS X

When you install a new app in OS X, it usually comes in the form of a DMG file that you have to then mount, eject, and delete. This simple Automator service simplifies the process, ejecting the disk image and deleting its DMG file in one fell swoop.

Here's how to create this very simple service:

  1. Open up the Automator application and select "Service".
  2. Change the top two dropdown menus to receive "No Input" from the "Finder".
  3. 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
  4. Save the service as whatever you want (like "Eject & Delete DMG") and exit Automator.

Now, when you're done using a DMG file, you can just click on it, then go to Finder > Services > Eject & Delete DMG to eject the image and delete the DMG with one click. If you really want to make it fast, you can go to System Preferences > Keyboard > Keyboard Shortcuts and give your service a handy keyboard shortcut. Note that this doesn't work if you select the DMG in the Finder sidebar; you have to select it in an actual Finder window (or on the desktop). Hit the link to read more.

Auto-Delete a DMG File When Ejecting a Disk Image [Mac OS X Hints]


Comments

    Handy tool - thanks.

    Hi
    I didn't know you had to delete the 'DMG' files, I just recovered 2GB on my hard drive.
    Thanks for that.
    My question is: can you also delete the 'mpkg' files?
    Please answer :)
    Thanks!

Join the discussion!

Trending Stories Right Now