How to Automatically Eject and Delete DMG Files in Mac

Mounted Mac disk image

One of the aspects where Macs differ from Windows PCs the most is when it comes to installing apps. On a Mac, you need to mount the disk image and then unmount and delete it after the installation is complete, which can be a bit complicated, especially if you have to test a lot of apps in a short amount of time.

Come to think of it, this time we’re going to share some neat little workflows you can deploy on your Mac using Automator. With any of them, every time you finish installing the app on your Mac, you’ll be able to eject/unmount and delete the disk image with just a few clicks.

Let’s start with Automator’s how to set up these beautiful workflows.

Automatically delete DMG files when you eject them

Step 1: Open Automator and choose to create a new document. From the available document types displayed on the dialog box, select Service.

Automation service

Step 2: At the top of the right panel, make sure to select from the options drop-down menu ‘no input’ And ‘Find Tool’ respectively so that the final result looks like the image below.

Get automation service

Step 3: Next, on the left panel of Automator, search for Run AppleScript action and drag it to the right panel. The AppleScript window will display with some placeholder code in it.

Automatically run ApplescriptAuto code 1

Remove that code and instead copy and paste the following into the script box:

tells the application “Finder” to set selection_list to a dialog showing selectif (count selection_list) ¬”Please select the mounted drive from the disk image.” titled ¬”No selection found” with stop icon button [“OK”] button defaults 1returnend ifset my_selection to item 1 of selection_listset my_kind to the type of my_selectionset my_name to the name of my_selection if my_kind is not “Volume” the dialog box displays ¬”Please select a mounted volume from the disk image file.” titled “Selection is not Disk Image” with stop icon button [“OK”] node defaults 1returnend ifset volume_list to fragments of (do shell script “hdiutil info | grep ^/dev/disk | grep -o ‘/Volumes/.*'”) sets source_list to fragments of (do shell script “hdiutil info” | grep ^ image’-‘alias | grep -o ‘/.*'”) set match_found to falserepeat with v from 1 to (count volume_list)if “/Volumes/” & my_name = item v of volume_list then set match_found to trueexit repeatend ifend repeatif match_found is not equal to true and then display the dialog box ¬”The selected drive does not appear to be a Disk Image.” titled “Cannot find Disk Image” with stop icon button [“OK”] node defaults 1returnelseset my_source to POSIX file (item v of source_list) as alias moves my_source to trash my_selection–show my_sourceend ifend tell

See more:  How to Change Search Engine From Yahoo to Google on Mac

Step 4: Now save this Automator service and give it a memorable name.

Auto push and delete

Step 5: Once done, every time you mount the disk image, all you have to do is select it and on the Finder menu select Service And after that Remove and Delete (or whatever you named the service you just created) and the disk image file will be unmounted and deleted with a single click.

push and delete

Now, let’s look at another Automator workflow that achieves the same goal, but in complete opposite.

Automatically remove DMG files when you drag them to the trash

As you can see from the title, this Automator workflow allows you to achieve the same purpose, except the opposite, so you can avoid this message every time you drag the mounted DMG file into the bin. garbage.

Disturbing messages

Here are the steps to create it.

Step 1: Create a new document in Automator and select Folder Actions from the available documents.

Automatic folder action

Step 2: At the top of the right panel, select Other… from the drop-down menu. Then on the dialog that pops up, type ~/.Recycle Bin to work with that directory.

Automatic Machine OtherAutomatically go to folderAutomatic Trash Bin

Step 3: Next, on the left panel, drag the . icon Run Shell Script action to the right panel. On the two drop-down menus that appear, select /usr/bin/python And as argument corresponding.

Automator Run Shell Scriptautomation field

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

enter string, os, syslines = os.popen(“hdiutil info”).readlines() should_eject = Falsefor 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 = Trueelif line.startswith(“/dev/”) and should_eject is True:os.popen(“hdiutil eject %s” % line.split ()[0])should_eject = Falseelif line.startswith(“###”):should_eject = False

See more:  Resolve System Restore Disabled By Group Policy Error Windows

Once done, save the Folder Action and exit Automator. Now, whenever a DMG file is mounted, all you have to do is drag it to the Trash and it will also be unmounted.

Automatic code 2Automator New Folder Action

Hot Tip: You can also create keyboard shortcuts for these actions by following the instructions at the end of this tutorial.

And there you go. Two different workflows to enable a very convenient feature on your Mac. Now all that’s left to do is choose which one you find more convenient. And best of all? In either case, you’ll learn a little more about Automator. Enjoy!

Categories: How to
Source: thpttranhungdao.edu.vn/en/

Rate this post

Leave a Comment