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

Click here to return to the 'Updated script for Leopard' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Updated script for Leopard
Authored by: carmst on Feb 10, '09 06:27:41AM
After I upgraded to OSX 10.5 I had to update this script, because 10.5 was killing it halfway through if too many small files were in the trash, probably due to too much disk I/O. The old version admittedly was grossly inefficient, so criticism accepted, Leopard. :)

The version below now only gets the size of the Trash folder once, instead of after every file is deleted. There's also some logging and a retry with admin privileges if a stubborn file fails to delete.

on adding folder items to this_folder after receiving added_items
	set sizeLimit to 2048
		tell application "Finder"
			set theSize to (size of (this_folder)) / (1024 ^ 2)
			repeat with thisItem in added_items
				set modification date of thisItem to (current date)
			end repeat
		end tell
	end try
	logConsole("TrashControl: Trash is using " & theSize & " of " & sizeLimit & " MB.")
	do shell script "ls -tr " & (quoted form of (POSIX path of this_folder))
	set theFiles to paragraphs of result
	set i to 1
	repeat while theSize is greater than sizeLimit
		set oldestFile to alias ((this_folder as text) & (item i of theFiles))
			set theSize to theSize - ((size of (info for oldestFile)) / (1024 ^ 2))
		end try
		logConsole("TrashControl: Deleting " & quoted form of (POSIX path of oldestFile))
		set sh_script to "rm -Rf " & quoted form of (POSIX path of oldestFile)
			do shell script sh_script
		on error errmsg
			set dialogmsg to "File failed to delete. Retry as Admin?"
			display dialog dialogmsg
			do shell script sh_script with administrator privileges
		end try
		set i to i + 1
		logConsole("TrashControl: Trash is using " & theSize & " of " & sizeLimit & " MB.")
	end repeat
	logConsole("TrashControl: Trash is under control at " & theSize & " MB.")
end adding folder items to

on logConsole(logMsg)
	set shellCommand to "logger" & space & quoted form of logMsg
	do shell script shellCommand
end logConsole

[ Reply to This | # ]