Q:

How can I have dmg files automatically unmount when I trash them?

A while ago, I used to have some kind of App or service that would automatically unmount a dmg file whenever I put the dmg file into the trash. This was really convenient, but I've since reinstalled OS X, and I've completely forgotten the name of the app that provides this useful feature. I still have backups of my old system, but I have no idea where to look. So, does anyone know of an app that provides this feature?
(Note: There are plentiful tutorials on how to achieve the reverse, i.e. auto-trash on eject, but this is not what I'm looking for.)

A:

I found two options

Use a folder action on ~/.Trash to unmount any DMG getting moved into the trash can (see second part of this article, summarized below)
DMG Cleaner claims to be able to do this as well

Folder Action to unmount DMG moved to Trash

Create a new "Folder Action" workflow in Automator
Set Folder Action to work on ~/.Trash
Add a "Run Shell Script" action to the workflow 
Set shell to "/usr/bin/python" and make sure input gets passed as arguments
Replace the content of the script box with the following script
import string, os, sys
lines = os.popen("hdiutil info").readlines()
should_eject = False
for line in lines:
    if line.startswith("image-alias"):
        path = line.split(":")[1]
        image_path = path.lstrip().rstrip()
        if image_path in sys.argv:
            should_eject = True
    elif line.startswith("/dev/") and should_eject is True:
        os.popen("hdiutil eject %s" % line.split()[0])
        should_eject = False
    elif line.startswith("###"):
        should_eject = False

Save the workflow

