How to Automatically Eject and Delete DMG Files in Mac

How to Automatically Eject and Delete DMG Files in Mac

How to Automatically Eject and Delete DMG Files in Mac

Macs differ from Windows PCs in the aspect of installing applications. On Macs, you need to mount a disk image and then unmount and delete it once the installation is finished, which can be a hassle, especially if you have to test many apps in a short time.

Therefore, we’ll share a couple of workflows using Automator on your Mac. With these workflows, you can eject/unmount and delete the disk image with just a couple of clicks after completing an app installation.

Now, let’s learn how to set up these Automator workflows.

Delete DMG Files Automatically When You Eject Them

Step 1: Open Automator and create a new document. Choose to create a Service.

On the dialog box that appears, select “Service” from the available document types.

How to Automatically Eject and Delete DMG Files in Mac

Step 2: At the top of the right panel, choose ‘no input’ and ‘Finder’ from the dropdown menus.

Step 3: On the left panel of Automator, search for the ‘Run AppleScript’ action and add it to the right panel. Delete the placeholder code in the AppleScript window and replace it with the following code:

tell application “Finder”

set selection_list to selection

if (count selection_list) is 0 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 ¬

See also  6 Best iPhone 15 Cases With Card Holder

“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

Step 4: Save the Automator service with an easily memorable name.

Step 5: Whenever a disk image is mounted, select it and go to Finder > Services > Eject and Delete (or the service name you created) to unmount and delete the disk image file in one click.

How to Automatically Eject and Delete DMG Files in Mac

Now, let’s examine another Automator workflow that achieves the opposite objective.

As you can see from the title, this Automator workflow allows you to achieve the same purpose, except in reverse. This way, you can avoid this message every time you drag a mounted DMG file to the trash. Here are the steps to create it. Step 1: Create a new document in Automator and select Folder Action from the available document types.

See also  2 Addictive Free iOS Games to Improve Vocabulary & Math Skill

How to Automatically Eject and Delete DMG Files in Mac

Step 2: At the top of the right panel, select “Other…” from the dropdown menu. Then, on the dialog box that pops over, type “~/.Trash” to work with that folder.

Step 3: Next, on the left panel, drag the “Run Shell Script” action to the right panel. On the two dropdown menus that show up, select “/usr/bin/python” and “as arguments” respectively.

Step 4: Replace the placeholder script in the script box with the following one.

Once done, save the Folder Action and quit Automator. Now, whenever a DMG file is mounted, simply drag it to the Trash and it will be unmounted simultaneously.

How to Automatically Eject and Delete DMG Files in Mac

Cool Tip: You can create keyboard shortcuts for these actions by following the instructions at the end of this tutorial. And there you go. Two workflows to enable a convenient feature on your Mac. Now all you have to do is choose which one you find more convenient. And the best part? In both cases, you’ll learn more about Automator. Enjoy!

Leave a Comment