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


Click here to return to the 'An AppleScript to create a size-limited trash can' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to create a size-limited trash can
Authored by: carmst on Nov 07, '06 10:56:35AM
Thanks, the -k option was exactly what I gave up looking for, and I had no idea mac filenames permitted quotes.

I've updated the script for those changes, and there's now a check to see if any of the incoming items exceeds the limit, so one item doesn't wipe your trashcan.


on adding folder items to this_folder after receiving added_items
	try
		-- Size limit of the Trash folder in MB
		set trash_limit to 2048
		
		--  Check size & Touch all incoming items to update the modified date to now
		repeat with i from 1 to the count of added_items
			set a_file to item i of added_items
			
			-- Get incoming item size and check if it exceeds the max trash size
			set a_file_size to ((do shell script "du -sk " & quoted form of (POSIX path of a_file) & "| awk '{print $1}' ") / 1024)
			if a_file_size is greater than trash_limit then
				display dialog quote & (name of (info for a_file)) & quote & " is too big for the Trash. Would you like to delete it permanently?" buttons {"Yes", "No"} default button "No"
				set response to the button returned of the result
				if the response is "Yes" then
					-- Permanently delete
					set sh_script to "rm -Rf " & quoted form of (POSIX path of a_file)
					do shell script sh_script
				end if
				if the response is "No" then
					-- Rename and move to Desktop
					set sh_script to "cd ~/Desktop;mv " & quoted form of (POSIX path of a_file) & " " & quoted form of (POSIX path of a_file)
					do shell script sh_script
					display dialog quote & (name of (info for a_file)) & quote & " has been moved to the Desktop."
				end if
			end if
			if a_file_size is not greater than trash_limit then
				set sh_script to "touch " & quoted form of (POSIX path of a_file)
				do shell script sh_script
			end if
		end repeat
		
		-- Get the current Trash size in 1k clusters, then divide by 1024 to get megabytes
		set trash_size to do shell script "du -sk ~/.Trash/ | awk '{print $1}'"
		set trash_size to trash_size / 1024
		
		-- Delete old items until we're under the limit
		repeat while trash_size is greater than trash_limit
			set trash_files to (list folder this_folder without invisibles)
			set oldest_file to {}
			
			-- Get the least recently deleted file in the Trash folder
			repeat with i from 1 to the count of trash_files
				set a_file to alias ((this_folder as text) & (item i of trash_files))
				if i is equal to 1 then set oldest_file to a_file
				if the (modification date of (info for a_file)) comes before (modification date of (info for oldest_file)) then set oldest_file to a_file
			end repeat
			
			-- Delete the file and update the Trash icon
			set sh_script to "rm -Rf " & quoted form of (POSIX path of oldest_file)
			do shell script sh_script
			
			-- And rinse!
			set trash_size to do shell script "du -sk ~/.Trash/ | awk '{print $1}'"
			set trash_size to trash_size / 1024
		end repeat
		tell application "Finder" to update trash
	end try
end adding folder items to


[ Reply to This | # ]
An AppleScript to create a size-limited trash can
Authored by: aqsalter on Nov 11, '06 11:09:44PM
small problem... where it says

	-- Delete the file and update the Trash icon
	set sh_script to "rm -Rf " & quoted form of (POSIX path of oldest_file)
	do shell script sh_script
this will fail with no warning if you don't have permission to delete the file... it should read

	-- Delete the file and update the Trash icon
	set sh_script to "rm -Rf " & quoted form of (POSIX path of oldest_file)
	try
		do shell script sh_script
	on error
		display dialog "Permission denied on file " 
			& quoted form of (POSIX path of oldest_file) 
			& " would you like to try with administrator priviledges?"
		do shell script sh_script with administrator privileges
	end try


[ Reply to This | # ]