tell application "Finder"
delete every item of folder "path:to:folder" whose modification date is less than ((get current date) - 7 * days)
end tell
However, folder actions trigger based on events such as dropping files in the folder or opening the folder window.
They don’t run on any kind of timed schedule so you can’t arbitrarily remove files that are older than 7 days. However you could have the script run on the other events (e.g. each time a file is added to the folder it deletes old files), or you could use some other script scheduling system to run the script on any period you like.
You could also have a stay-open script that runs at an interval specified in the script
on idle
try
tell application “Finder”
delete (every item of folder “path:to:folder” whose modification date is less than ((get current date) - 7 * days))
end tell
end try
return 86400 – idle time, in seconds, between executions
end idle
Ok, I have found multiple references to deleting files after a certain date, but I can never make any of them work, and I don’t understand why.
So if I am using the code seen here…
try
tell application "Finder"
delete (every item of folder "drivename:desktop:foldername" whose modification date is less than ((get current date) - 7 * days))
end tell
end try
return 86400 -- idle time, in seconds, between executions
end idle
Why aren’t my files deleting? The only thing I have changed, is the “pathname” info, and I have altered that in the example above to show my understanding of it, combined with where my directories/folders are located.
Thanks,
Schmelkus
Model: g4 500 with Sonnet Encore ST/g4 Duet Upgrade
AppleScript: 1.9.3, I think
Browser: Safari 312
Operating System: Mac OS X (10.3.9)
I collect my screen captures in a folder and use a folder script like this to keep it cleaned. But if I show invisible files and the script is activated at the same time the folder icon file gets deleted too.
Quite aside from an AppleScript folder action to do it, there is always Noodlesoft’s Hazel, which over many years now, I’ve always found to be bombproof. What action it performs can be a script. Downside, of course, is that it costs $29 but does offer a free trial. I switched back in the days when folder actions always seemed to be a bit iffy and never switched back.