Joseph Paul

Joseph Paul

Eject and delete DMGs in one strike

February 1, 2017 #hack

Opening a downloaded .dmg file goes fairly quick, straight from the browser. Unmounting it, once the .app is copied to the applications folder, also goes pretty quick using CMD+E. But then, there's still that dmg file in the downloads folder, forcing you to navigate there in Finder and manually deleting it.

Not when you have CMD+SHIFT+E linked to this AppleScript, set up as an Automator Service for Finder!

1. Create a new Service in Automator

Create a new "Service" in Automator

2. Service receives no input in

Set the service to receive no input in

3. Drag in a new Run AppleScript block

Drag in a new Run AppleScript block

4. Paste in the code

tell application "Finder"
    set my_selection to (target of front Finder window)
    set my_name to name of my_selection
    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
        set my_source to POSIX file (item v of source_list) as alias
        eject my_name
        move my_source to the trash
    end if
end tell

5. Configure the keyboard shortcut in System Preferences > Keyboard