Jump to content
BermudaLamb

Empty Trash Files Older Then NN Days

Recommended Posts

I'm trying to find a workflow that finds all files in my Trash folder older than xx days and force deletes them. Does anyone know of such a workflow? Also, does anyone know how to schedule a workflow to be triggered every day at a specific time?

 

My ultimate goal is to replace Hazel with Alfred workflows.

Share this post


Link to post

I'm not sure Alfred is much of a replacement for Hazel. They're designed to work in fundamentally different ways, and Alfred really isn't suited for running/managing background jobs.
 
If you want to schedule a job, you should write a command-line script to perform whatever action and then create a Launch Agent for it or a cron job to run it at whatever interval.
 
Personally, I use LaunchControl to create and manage Launch Agents because I prefer launchd to cron.
 
If you can encapsulate whatever it is you want to do in a script, you can then run that from Alfred, Hazel or launchd/cron.
 
With regard to your Trash question, I don't think there are any APIs, metadata or utilities that can help you there. Hazel appears to keep track itself of how long each file has been in the trash. So to replace it, you'll probably have to write a utility that can do the same…

Share this post


Link to post

Because I am a learner, I collect lots of workflows and scripts just to see how things work. Sometimes a gem comes along.

I have this little Applescript that I found very useful. Its pretty well documented. I suppose you could use it in Alfred but I do not know how to do it.

You have to set up Trash Folder Actions and put the Applescript in your Library/Scripts/Folder Action Scripts

 

EDIT: I did not write this script and I do not remember who did or where I got it.

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
Edited by Colin

Share this post


Link to post

I don't think you can attach folder actions directly to Trash. It's a "magic" folder that doesn't actually exist.
 
You could try attaching the script to the underlying ~/.Trash and .Trashes/<UID> directories at the root of each volume.
 
Note that srm securely deletes files by overwriting them. If you just want them gone, rm is much faster.

Share this post


Link to post

When you show hidden files, you can see the folder Trash in the users directory. When you right click you can setup Folder Actions on it.

I don't think its magic, just hidden.

Share this post


Link to post

It's not that simple. Like I said above, the directory is called ~/.Trash, not ~/Trash (its name starts with a period—that's what makes it hidden), and it's not the only directory that contains trashed files.

Share this post


Link to post

Now I'm totally confused. If I send  files to Trash and I can see these files in the Trash folder, do you mean they are not really there. I honestly don't understand  :unsure:

 

notrash.png

Share this post


Link to post

TL;DR What you're suggesting is a very clever solution. What I'm saying is that it might not be complete, depending on system configuration. It absolutely should work; you just might need to add actions to a few more folders.

Finder sometimes lies about files and especially their names.
 
If you open the real Trash in Finder and do Get Info, then open the Trash/.Trash hidden directory in your home directory and also do Get Info, you'll see they're not the same. In particular, there is no "Where" for the real Trash because it isn't a real folder:
 
TILLzFr.png
 
You can see that the "Trash" directory in your home directory is really called ".Trash" (which is what you see in a terminal). More importantly, you can see that not all files that are in the Trash are actually in the ~/.Trash folder.
 
Any files you trash are moved to a special directory on the same partition. In the case of the partition your home directory is on, that's ~/.Trash. On other partitions, a subdirectory of the .Trashes directory at the root of the partition is used. The "missing" files in the screenshot are three directories, totalling a couple of gigabytes, that were on a different partition (/Volumes/Media in this case), so they are now in the /Volumes/Media/.Trashes/501 directory on that partition (501 is my user ID), not in ~/.Trash.
 
That is to say, adding a folder action to ~/.Trash will work if your computer only has one drive and partition. If it has more, you will need to also add folder actions to the other directories that contain trashed files, otherwise they will sit in the Trash forever.

Edited by deanishe

Share this post


Link to post

That makes it clearer. I thought that when my Trash was empty it was actually empty. That was until last night when I plugged in my external drive. 

Thanks for education, I really appreciate the time you have taken to explain it.

 

 

This is a much smaller script that works well. Hope BermudaLamb can use it. Change the +5 to whatever days you want.

on adding folder items to this_folder after receiving added_items
	
	set files2delete to paragraphs of (do shell script "find " & quoted form of POSIX path of this_folder & " -type f -mtime +5 -delete")
	
end adding folder items to
Edited by Colin

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...