Securely empty trash via AppleScript after 'n' days

Feb 09, '09 07:30:02AM

Contributed by: cakeria

I'm fairly new to the OS X world, and noticed that I kept forgetting to empty my trash. That's because, with Windows, I got used to using Shift-Delete, which deletes immediately. I figured that it's better to hold on to those files a few more days before throwing them out, though, so I found my self in need of a script to do this.

I started to look around and found two scripts here on this site:

Although these are very useful scripts, I wanted a script that deleted on the basis of time (like the first one), but does not need to run (the first one needs a cron job) when it's not needed (it's fast, but still..). The limitation of not running unnecessarily rules out these two applications as well: While trashtimer runs a constant process in the background, Compost does that and even costs money (and I don't like that ;) ). So I decided to write my own script.

This script will only run when you delete a file, and it's not very complicated but it works. It securely deletes files in the trash after seven days (you can pick any number you like; this works for me because I know I'm not going to use the files anyway after having them in the trash for seven days). Here's the code:

on adding folder items to this_folder after receiving added_items
  try
    -- Set number of days to wait
    -- Change this to the number of days you want
    set time_diff to 7
    
    -- Touch all incoming items to update the modified date to now
    repeat with i from 1 to the number of items in the added_items
      set a_file to item i of added_items
      set sh_script to "touch " & quoted form of (POSIX path of a_file)
      do shell script sh_script
    end repeat
    
    set trash_files to (list folder this_folder without invisibles)
    -- Set variable "use_date" to the current date to be a little bit more efficient when working with many files
    set use_date to (current date) - time_diff * days
    
    -- Search through the trash and delete files that are there longer than 7 days
    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 the (modification date of (info for a_file)) comes before use_date then
        -- "srm -sf" deletes files in a secure way in "simple mode" use "man srm" if you want different modes
        set sh_script to "srm -sf " & quoted form of (POSIX path of oldest_file)
        do shell script sh_script
      end if
    end repeat
  end try
end adding folder items to
Save the code as a script in the folder /Users -> your_username -> Library -> Scripts -> Folder Action Scripts, then open the app Folder Actions Setup (in the Applications -> AppleScript folder), and manually add the folder ~/.Trash as an action folder, and then add this script to it. Then try trashing! (If you're lazy, trashtimer will be the better option for you, because it's even easier to install.)

[robg adds: I haven't tested this one. As with other trash-related scripts, keep in mind this will only work for files in the trash on your boot drive or partition; if you've got more than one drive or partition, you'll need to modify the script to work properly with the other trash cans.]

Comments (9)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20090205033328460