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 3.00 / 5
  You rated: 5 / 5 (5 votes cast)
 
[8,269 views]  

Auto-delete a DMG file when ejecting a disk image | 11 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 | # ]
Auto-delete a DMG file when ejecting a disk image
Authored by: tmoswift on Dec 29, '13 11:42:21PM

Lots of good points in this thread. I've re-thought how this should work.

My problem is that my downloads folder gets cluttered with dmg files. I can't remember which ones I've already installed. Some of them I have opened and installed, but I've forgotten to move them out of my downloads folder. Some of them I downloaded but haven't opened yet.

My new version of the script moves the dmg file when it is mounted. To use it, save this script to '/Library/Scripts/Folder Action Scripts/' and attach this folder action script to the /Volumes directory. Ideally, my script would trigger when you eject the disk image, but by then it's too late; the disk image is gone, and there's nothing left for AppleScript to process. I figure triggering on mount was pretty good, since I really wanted to know what dmg files I had opened or not.

Most of you didn't want to delete the dmg file. That's a good idea. My script now moves it to a dmg_archive folder. Feel free to change this to suit your needs.


-- When a disk image mounts, move the dmg file to a particular folder (e.g. an archive of dmg files).
on adding folder items to this_folder after receiving these_items
	
	-- Setup the folder you want to place the dmgs in after unmounting them.
	-- By default this is a folder called "dmg_archive" in your downloads folder.
	set save_path to check_for_folder(path to downloads folder, "dmg_archive")
	
	-- get mount path and source file path info about current mounted disk images 
	set dmg_info to get_dmg_info()
	
	repeat with i from 1 to (count these_items)
		tell application "Finder"
			set my_name to name of item i of these_items
			repeat with v from 1 to count dmg_info
				-- match volume mount point paths to verify we are targeting the right disk image
				if "/Volumes/" & my_name = item 1 of item v of dmg_info then
					-- item 2 of item v is a text string describing the source file path
					set my_source to POSIX file (item 2 of item v of dmg_info) as alias
					move my_source to save_path
					exit repeat
				end if
			end repeat
		end tell
	end repeat
end adding folder items to


on get_dmg_info()
	
	-- create temporary list which will contain pairs of Disk Image Mount Paths and dmg Source File Paths
	set dmg_mount_source to {}
	
	-- make a temp file to store our plist data in
	set hdiutil to do shell script "mktemp -t dmg_move"
	
	-- dump information from hdiutil into our temp file
	do shell script "hdiutil info -plist > " & hdiutil
	
	-- use System Events to process our plist data
	tell application "System Events"
		set disk_images to property list items of property list item "images" of property list file hdiutil
		
		repeat with i from 1 to (count disk_images)
			-- this is the path to our dmg source file
			set dmg_path to value of property list item "image-path" of item i of disk_images
			
			-- not sure what a system entity is and why a disk image sometimes has multiple dicts: (partition scheme, partiontion map, etc)
			set sys_ents to property list items of property list item "system-entities" of item i of disk_images
			
			-- we just have to find the one that contains our mount-point information
			repeat with j from 1 to (count sys_ents)
				set mount_point to ""
				try
					set mount_point to value of property list item "mount-point" of item j of sys_ents
				end try
				if mount_point is not "" then
					-- add information about our disk image to our temporary list of mount path / source file pairs
					set dmg_mount_source's end to {mount_point, dmg_path}
					exit repeat
				end if
			end repeat
			
		end repeat
		
	end tell
	
	-- delete our temporary hdiutil plist file
	do shell script "rm " & hdiutil
	
	return dmg_mount_source
	
end get_dmg_info


on check_for_folder(enclosing_folder, fol_name)
	-- Used to check if a folder exists, and if not create it.
	-- Returns path to folder.
	tell application "Finder"
		if not (exists folder fol_name of enclosing_folder) then
			set fol_path to make new folder at enclosing_folder with properties {name:fol_name}
		else
			set fol_path to folder fol_name of enclosing_folder
		end if
	end tell
	return fol_path as alias
end check_for_folder
[code]


[ Reply to This | # ]