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

Securely empty trash via AppleScript after 'n' days System
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.]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (6 votes cast)
 
[11,162 views]  

Securely empty trash via AppleScript after 'n' days | 9 comments | Create New Account
Click here to return to the 'Securely empty trash via AppleScript after 'n' days' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Securely empty trash via AppleScript after 'n' days
Authored by: Dr. T on Feb 09, '09 10:41:58AM

You can simplify the script and eliminate the need to have a newly trashed file as the trigger. Strip the script down so that it securely empties older files and does nothing else. Then, go to automator, make a new action, insert the AppleScript, and save it as an iCal plugin. Go to iCal and set it up as a daily recurring event at 3 am, for example. (You can do this without Automator, but I prefer plugin links to application links.) Your x-day old trash will be securely emptied daily (or whatever interval you choose in iCal.)

You can do this with other tasks. I have metered satellite internet, but the period from 1 am to 6 am is unmetered. I schedule large downloads (such as OS X updates) and bittorrent downloads using AppleScript and iCal.



[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: Dr. T on Feb 09, '09 11:03:21AM

I retract the above comment and add a warning.

I did not parse the script thoroughly. The script changes the modification date of every trashed file to the time of trashing.

WARNING: Changing the modification date is not a great idea for most files. For example, the modification data of an application or plug-in helps you know how old it is. If you accidentally trash it and then put it back, the modification date will be today, not two years ago. Some programs scan modification dates to see if the app or plug-in needs updating; such programs will be misled.

I recommend another approach. Create a text file for the Trash. Use a Folder Action Applescript to record the name of each file and the date it was trashed. At regular intervals, read the text file, find the files old enough for Trashing, see which ones are in the trash, and delete (or secure delete) them. Amend the trash file to include only what's now in the Trash. That should do it.



[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: Anonymous on Feb 09, '09 11:44:25AM

So you advocate engineering a whole new "Trash" schema, just because you consider throwing a file in the trash doesn't constitute "modifying" it?

Fair enough; each to their own.



[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: Brock Lee on Feb 09, '09 11:07:34AM

I don't believe this script works as described.

The script uses the date/time of the last modification to the file to determine whether it qualifies for deletion. The script does not use the date/time when the file was thrown away (i.e., put in the trash) to determine whether it qualifies for deletion.

Putting something in the trash does not alter the modification date/time.

So you could put something in the trash and find it permanently gone if it's been a while since you modified it.

I don't know AppleScript but I do know UNIX. Perhaps the modification time works differently under AppleScript. So, can anyone verify or challenge my claim?

[ Reply to This | # ]

Securely empty trash via AppleScript after 'n' days
Authored by: Stef@nK on Feb 09, '09 11:34:52AM
The UNIX touch command changes the modification date to the current date

[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: Brock Lee on Feb 10, '09 03:56:46PM
Thanks! I overlooked that it was touching the files as they went in.

[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: baltwo on Feb 09, '09 05:19:19PM

This isn't a PC. Stop using the Trash as a storage locker. CMD+SHIFT+delete is as simple to use as SHIFT+delete.



[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: osxpounder on Feb 10, '09 11:27:49AM

Nonsense. Use the Trash as a storage locker if you want. It's clearly designed to store files until you want, or must, delete them.

Thanks, though, for the keyboard shortcut reminder. I like knowing about those.



[ Reply to This | # ]
Securely empty trash via AppleScript after 'n' days
Authored by: drudus on Feb 11, '09 06:59:00PM
Or just use Hazel. It has options to clear out the Trash after a week etc.

[ Reply to This | # ]