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: wallybear on Nov 07, '06 08:19:36AM

Two suggestions:

1) Size of clusters is not necessarily 2048 bytes, so it's better to add the -k parameter, that gives you the block count using 1Kb blocks (i.e. the size in kilobytes):

change the command
du -s ~/.Trash/ | awk '{print $1}'
with
du -sk ~/.Trash/ | awk '{print $1}'

and then remember to divide by 1024 instead of 2048.

2) your script doesn't handle correctly filenames containing quotes. You can fix this replacing every occurence of

quote & (POSIX path of oldest_file) & quote

with

quoted form of (POSIX path of oldest_file)

that will handle for you all cases.



[ Reply to This | # ]
An AppleScript to create a size-limited trash can
Authored by: dipsomaniac on Nov 07, '06 10:12:47AM
Trash Timer is my solution. One of the things I like about a desktop over a laptop is having a large enough hard drive so you don't have to worry about emptying the trash often. I also only throw away things I'm absolutely sure I don't need. Anything I think there is the slightest possibility I might use again goes into a separate folder until I know I don't need it.

[ Reply to This | # ]
An AppleScript to create a size-limited trash can
Authored by: dipsomaniac on Nov 07, '06 10:14:41AM

And that was supposed to be a separate entry, not a reply to the first post per se.



[ Reply to This | # ]
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 | # ]