BermudaLamb Posted November 5, 2015 Posted November 5, 2015 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.
deanishe Posted November 5, 2015 Posted November 5, 2015 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…
Colin Posted November 9, 2015 Posted November 9, 2015 (edited) 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 November 9, 2015 by Colin
deanishe Posted November 9, 2015 Posted November 9, 2015 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.
Colin Posted November 10, 2015 Posted November 10, 2015 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.
deanishe Posted November 10, 2015 Posted November 10, 2015 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.
Colin Posted November 10, 2015 Posted November 10, 2015 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
deanishe Posted November 11, 2015 Posted November 11, 2015 (edited) 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: 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 November 11, 2015 by deanishe Colin 1
Colin Posted November 11, 2015 Posted November 11, 2015 (edited) 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 November 12, 2015 by Colin
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now