Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Auto-delete a DMG file when ejecting a disk image System
While the Mac App Store is great, I still find myself downloading .dmg files and installing plenty of third-party software. I often forget to delete .dmg files after I've installed applications. Here's a script to automatically move the .dmg file to the trash when you eject the disk image.

To use this script, select the mounted volume in the Finder (or in the "Devices" section of the Finder sidebar), and trigger this script via your favorite macro app instead of ejecting the disk image normally.
tell application "Finder"
	set selection_list to selection
	if (count selection_list) < 1 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 ¬
			"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
Matt Rajca has a script that does the reverse: ejects a Disk Image volume when you trash the .dmg file.

Could you simply have a search that looks for old .dmg files in your Downloads folder? Yes, though I often forget to open and install .dmg files right away, so I prefer linking the delete action to the eject.

[kirkmc adds: This works for me if I select a volume in a Finder window, but not in the sidebar. This hint was submitted before Mountain Lion, so perhaps there's something that needs to be tweaked for 10.8. I figured it is worth posting so you all can find what to fix; I'll update the script in the hint if someone provides a solution in the comments.]
    •    
  • Currently 2.33 / 5
  You rated: 5 / 5 (3 votes cast)
 
[5,350 views]  

Auto-delete a DMG file when ejecting a disk image | 10 comments | Create New Account
Click here to return to the 'Auto-delete a DMG file when ejecting a disk image ' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Yeah - what could possibly go wrong?
Authored by: Westside guy on Aug 10, '12 09:51:06AM

This just seems like a really bad idea, implemented well.



[ Reply to This | # ]
Why not keep the DMG's?
Authored by: ObbieZ on Aug 10, '12 10:05:40AM

If things go horribly wrong and you need to rebuild your system, the stock advice is to "reinstall your applications from original media." Now that DMGs have replaced optical disks, they ARE the original media you need to reinstall your apps if/when things go Bad.

I like to keep my latest and important DMGs in a dedicated folder that gets backed up.



[ Reply to This | # ]
Why not keep the DMG's?
Authored by: hamarkus on Aug 10, '12 02:16:38PM

I rather keep additional full backups (six currently) than store stuff I can download again in a few minutes if need be.



[ Reply to This | # ]
Delete after mounting
Authored by: PatrickS on Aug 10, '12 02:33:04PM

I usually delete (move to trash) the DMG as soon as I have double-clicked/mounted it. This ensures that it will get deleted sooner or later when I empty the trash.



[ Reply to This | # ]
Delete after mounting
Authored by: hamarkus on Aug 10, '12 03:47:58PM

I usually move them into the trash while the installer or the copy process is running, gets you something to do while you watch the progress bar.



[ Reply to This | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: redfood on Aug 10, '12 04:55:45PM

I don't know if I would call this auto-delete. Its more like close and delete at the same time by selected the close-and-delete script. You still have to remember to do something other than eject the volume.

From the title I was hoping for something that used folder actions or something to automatically delete a dmg on ejection without a workflow change.

Anyone have any ideas? Can this be done with folder actions?



[ Reply to This | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: david-bo on Aug 11, '12 03:48:46AM

I download my DMGs to /tmp where they automatically are cleaned out after a week (or at restart).

---
link



[ Reply to This | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: Sven G on Aug 12, '12 03:25:20AM

BTW, what happened to the so-called *internet-enabled disk images*?

That is, those that automounted, autocopied the application to the Downloads (or other) folder, autounmounted and then autotrashed themselves: not bad, at all!

Here's an old hint about those disk images:

http://hints.macworld.com/article.php?story=20030319081832804

IIRC, it's some time that they haven't worked anymore, at least from Lion and afterwards...



[ Reply to This | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: tingo on Aug 14, '12 07:19:27AM

Delete? I've been keeping all dmg's and all sorts of original application files, as well as downloaded fonts, on an external disk – all the way to pre-OS X days, I guess since 1988. Can't begin to tell how often this has saved my life.



[ Reply to This | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: Sven G on Aug 17, '12 04:08:28AM

Yes, of course it's a very good idea to keep the .dmg's, .zip's, etc. etc. that you might need in the future; but the internet-enabled disk images were cool, anyway (probably, they don't work anymore for security reasons; well, a .zip is almost equivalent, anyway)... ;-) :-)



[ Reply to This | # ]