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


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.
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 | # ]