#!/usr/bin/osascript property trash_limitd : 2048 # Size limit of the Trash folder in MB. property soundoff : false # Set this to the name of a sound file in "/Library/Sounds/" to make some noise or to 'false' for quiet. property logind : false # Switch to turn loging to Console on and off to playSound(s) # Play a sound in "/Library/Sounds/" if s is not false then try do shell script "nohup afplay " & quoted form of (((POSIX path of (path to "ssnd" from local domain)) & s) as Unicode text) & " > /dev/null 2>&1 &" end try end if end playSound to logConsole(logMsg) if logind is true and logMsg is not {} then try set logCommand to "logger " & quoted form of ("TrashLimiter: " & (logMsg as Unicode text)) do shell script logCommand end try end if end logConsole on adding folder items to this_folder after receiving added_items try set trash_limit to (trash_limitd * 1024) as number logConsole("Trash Limit is " & ((round (trash_limit / 1024)) & " MB")) # Check size & Touch all incoming items to update the modified date to now set edag to "" repeat with a_file in added_items set a_file to contents of a_file # 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) as Unicode text)) & " | awk '{print $1}'") as number if a_file_size is greater than or equal to trash_limit then logConsole((name of (info for a_file)) & " is too big at " & ((round (a_file_size / 1024)) & " MB")) tell application "Finder" 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) as Unicode text) try do shell script sh_script on error set edag to "Permission denied on file " & (name of (info for a_file)) & " would you like to try with administrator priviledges?" display dialog edag buttons {"Yes", "No"} default button "Yes" set auth_response to the button returned of the result if the auth_response is "Yes" then do shell script sh_script with administrator privileges logConsole(sh_script & "as Admin") delay 0.1 else # Rename and move to Desktop set sh_script to "mv " & quoted form of ((POSIX path of a_file) as Unicode text) & " ~/Desktop/" do shell script sh_script set edag to quote & (name of (info for a_file)) & quote & " has been moved to the Desktop." end if end try else # Rename and move to Desktop set sh_script to "mv " & quoted form of ((POSIX path of a_file) as Unicode text) & " ~/Desktop/" do shell script sh_script set edag to quote & (name of (info for a_file)) & quote & " has been moved to the Desktop." display dialog edag end if end tell logConsole({edag}) if sh_script does not start with "mv " then # Sounds are good playSound(soundoff) end if else set sh_script to "touch " & quoted form of ((POSIX path of a_file) as Unicode text) do shell script sh_script logConsole(sh_script) delay 0.1 end if end repeat # Get the current Trash size try set trash_size to (do shell script "du -sk " & (quoted form of ((POSIX path of this_folder) as Unicode text)) & " | awk '{print $1}'") as number on error errorMessage logConsole("error: Unable to get current trash size. " & errorMessage) end try logConsole("Trash is " & ((round (trash_size / 1024)) & " MB")) # Get a list of least recently deleted files in the Trash folder try set trash_files to do shell script "ls -tr " & (quoted form of ((POSIX path of this_folder) as Unicode text)) set trash_files to paragraphs of trash_files set i to 1 on error errorMessage logConsole("error: " & errorMessage) end try set oldest_file to {} # Delete old items until we're under the limit repeat while trash_size is greater than trash_limit # Get the least recently deleted file in the Trash folder try set oldest_file to alias ((this_folder as Unicode text) & (item i of trash_files)) logConsole(oldest_file) tell application "Finder" to set oldest_file_size to (get physical size of oldest_file) / 1024 on error errorMessage logConsole("error: " & errorMessage) end try # Delete the file set sh_script to "rm -Rf " & quoted form of ((POSIX path of oldest_file) as Unicode text) try logConsole(sh_script) do shell script sh_script delay 0.1 on error tell application "Finder" set edag to "Permission denied on file " & (name of (info for oldest_file)) & " would you like to try with administrator priviledges?" display dialog edag buttons {"Yes", "No"} default button "Yes" set auth_response to the button returned of the result if the auth_response is "Yes" then do shell script sh_script with administrator privileges delay 0.1 else # Rename and move to Desktop set sh_script to "mv " & quoted form of ((POSIX path of oldest_file) as Unicode text) & " ~/Desktop/" do shell script sh_script set edag to quote & (name of (info for oldest_file)) & quote & " has been moved to the Desktop." display dialog edag end if end tell end try logConsole({edag}) # And rinse! if oldest_file is not {} or null then set trash_size to (trash_size - oldest_file_size) logConsole("Trash is now " & ((round (trash_size / 1024)) & " MB")) set i to i + 1 else logConsole("Gibber, Gibber") exit repeat end if end repeat # Update the Trash icon tell application "Finder" to update trash if oldest_file is not {} and sh_script does not start with "mv " then # they tell you what is going on playSound(soundoff) end if on error errorMessage logConsole("error: " & errorMessage) end try end adding folder items to