Skip to content

Instantly share code, notes, and snippets.

@ruario
Last active November 27, 2022 22:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruario/b83c1030790948d66c624aa99203140c to your computer and use it in GitHub Desktop.
Save ruario/b83c1030790948d66c624aa99203140c to your computer and use it in GitHub Desktop.
A folder action to automount and extract downloaded .dmg files on macOS

This Automator folder action will monitor the Downloads folder for new dmg files and automatically unpack them into a similarly named sub directory. It requires no interaction from the user. On completion a notification will display.

As mounting and extraction happens in the background you save time, since you can continue to work. In addition with all disk image contents pre-copied to disk, moving any .app to "/Applications" is pretty much instant (or in the case of Vivaldi, you could run it in standalone mode directly from the created extraction folder).

To setup

  • Open the Automator.app (part of macOS) and create a new "Folder Action"
  • Choose the Downloads folder
  • Drag "Run Shell Script" (found under "Utilities") into the actions area
  • Change the Pass Input drop down to "as arguments"
  • Replace the example code with the following:
for DMG in "$@"; do
  # To unpack only Vivaldi dmgs, uncomment the next line and then comment out the one after it
  # if echo "$DMG" |  grep -qE '/Vivaldi\.([0-9]+\.){3}[0-9]+.*\.dmg$'; then
  if [ ${DMG: -4} == ".dmg" ]; then
    DIR="${DMG%.dmg}"
    if [ ! -e "$DIR" ]; then
      mkdir -p "$DIR/.mnt"
      yes Y | hdiutil attach "$DMG" -mountpoint "$DIR/.mnt" -nobrowse
      cp -R "$DIR/.mnt/" "$DIR"
      hdiutil detach "$DIR/.mnt"
      rmdir "$DIR/.mnt"
      osascript -e "display notification \"${DMG##*/}\" with title \"Extraction complete\" sound name \"Pop\""
    elif [ -d "$DIR/.mnt" ]; then
      # This is just to clean things up if something went wrong before
      hdiutil detach "$DIR/.mnt"
      rmdir "$DIR/.mnt"
    fi
  fi
done
  • Save the action with an appropriate name (e.g. "auto-unpack-dmgs")

To use

  • Download a dmg file and wait

Notes

This Automator folder action will auto-agree to any license. I am not a lawyer but I doubt that means the license does not apply. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment