Jump to content

Workflow to eject disk image and trash DMG after software install?


Recommended Posts

Not a workflow, but I did write a Service for that (so you can right-click on the disk image in Finder and do it that way).

 

Here's the Python script. Put this in a Run Script action with Language = /usr/bin/python input as argv:

import sys
import plistlib
from subprocess import Popen, PIPE

path = sys.argv[1]
dmg_path = None
info = Popen(["hdiutil", "info", "-plist"], stdout=PIPE).communicate()[0]
pl = plistlib.readPlistFromString(info)
for image in pl["images"]:
    for ent in image["system-entities"]:
        if ent.get("mount-point") == path:
            dmg_path = image["image-path"]
            break

if dmg_path:
    Popen(["hdiutil", "detach", path])
    Popen(["osascript", "-e", 'tell application "Finder" to delete POSIX file "%s"' % dmg_path])

You'll need to write your own Script Filter to find and list the mounted disk images, and pass the mount point (i.e. /Volumes/NAME) to the script.

Link to comment
6 minutes ago, mlondon said:

Also, as some installers use ZIP instead of DMG, perhaps a way to either accommodate for both DMG & ZIP or a separate workflow for ZIP.

 

For ZIP files, you could use a standard Alfred File Filter configured to search ~/Downloads for ZIP files and connect that to a Run Script action that deletes/trashes the file.

 

 

Edited by deanishe
Link to comment
  • 1 year later...

Thanks @deniashe, it works beautifully.

Is there any way to "inject" this workflow into the normal eject feature?

That means appending the keyword "eject" to this workflow as well, and when a user has both DMGs and volumes mounted, "eject" will list them all, and the subtext for DMGs will also include "... and delete".

Link to comment
5 hours ago, kodiak said:

Is there any way to "inject" this workflow into the normal eject feature?

 

The only thing you can do is set the workflow and the "eject" feature to use the same keyword. They'll all show up at the same time, but the disk images will be duplicated, and it probably won't work very well.

 

To get a satisfactory experience, Andrew would have to add the "…and trash" feature to Alfred or someone would have to add eject features to this one.

Link to comment
  • 3 years later...

This is very handy. I don't know anything about scripting, but I'm wondering if it would be a simple thing to create a version of that script that does the following:

 

Ejects all disk images (not disks). 

 

That's it. No selecting, no trashing, just a one-button disk image ejector. 

 

Failing that, I presume that deleting the second half of your script will do the ejecting part without the trashing part, as so:

 

log() {
    echo "$@" >/dev/stderr
}


log "name=$dmgname, mount=$dmgmount, path=$dmgpath"

# unmount
out="$( hdiutil detach "$dmgmount" 2>&1 )"
hds=$?
if [[ $hds -ne 0 ]]; then
    log "hdiutil exit status: $hds"
    log "$out"
    echo "$out"
    exit 1
fi

 

Edited by noisyneil
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...