I have a script that checks to see if any files are over 90 days old:
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
tell application "Finder"
try
set theFolderAlias to alias "Volumes:MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__changed:"
delete (get items of theFolderAlias whose creation date < ((current date) - 90 * days))
end try
try
set theFolderAlias to alias "Volumes:MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__removed:"
delete (get items of theFolderAlias whose creation date < ((current date) - 90 * days))
end try
end tell
There are an additional 12 more of these, and they all are identical except for the folders that they are looking at. I was helped with this script here sometime ago.
My question is this, does the “delete” actually delete the files or just move them to the trash. I’m hoping that it is a true “delete”, or will I need to add an “Empty Trash” at the end of the script. This script is looking at folders created by backup software, and if I wait the full 90 days I’ll find out, but thought I’d ask. Thanks.
Side note: Unlike POSIX paths which start with a slash representing the startup volume HFS paths start always with a disk name. If the path is on the startup volume it starts even with “Macintosh HD” or whatever the startup volume is named.
set theFolderAlias to alias "MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__changed:"
For several reasons, personally, I would take a bit of a different route. Generally I try to avoid scripting Finder if I can accomplish the same thing through System Events. Finder is notoriously slow dealing with files especially if there is a lot of them. Also, using System Events to delete files… the files get deleted immediately and do not go to the Trash.
With this route all you need to do is add the HFS paths of the folders you want to delete the files from, to the list of theFolders and use a repeat loop as shown below. Much less code this way and no need for the try command.
property theFolders : {"MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__removed:"}
tell application "System Events"
repeat with i in theFolders
delete (files of folder i whose ¬
creation date < ((my (current date)) - 90 * days))
end repeat
end tell
Thank you for the suggestion, much appreciated. I will be transitioning my script using yours as an example, and deleting the “Empty Trash” script at the end is an added bonus.
But, just so I understand, as I have 12 more folders that I’ll be looking at. The paths to the folders are in quotes (“path”) and separated by a (“path”, “next path”) comma, correct?
Yes that is correct. I only used the… " ¬ " in the code to avoid using all the folder paths on one line because it’s difficult to see everything without having to scroll all the way across.
property theFolders : {"MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Documents:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Dropbox Folders:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Dropbox Folders:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Installs:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Installs:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Movies:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Movies:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Music:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Music:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Photos Archive:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Photos Archive:__removed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Pictures:__changed:", ¬
"MacBook Pro Local Backups:SmartBackup Local Backups:Local Pictures:__removed:"}
tell application "System Events"
repeat with i in theFolders
delete (files of folder i whose ¬
creation date < ((my (current date)) - 90 * days))
end repeat
end tell
I also have a remote backup, and a simple find/replace in Script Debugger and it’s done. Thanks for your help with this. I’ve said it here before, but I’ll say it again. It’s always a good day when I learn something new. Today was a good day. Thanks again.
The paths contain a lot of redundancy. This does the same. And it’s also not necessary to calculate the date in each iteration of the loop.
property theFolders : {"Documents", "Dropbox Folders", ¬
"Installs", "Movies", "Music", "Photos Archive", "Pictures"}
set basePath to "MacBook Pro Local Backups:SmartBackup Local Backups:Local "
set threeMonthsAgo to (current date) - 90 * days
tell application "System Events"
repeat with i in theFolders
delete (files of folder (basePath & i & ":__changed:") whose creation date < threeMonthsAgo)
delete (files of folder (basePath & i & ":__removed:") whose creation date < threeMonthsAgo)
end repeat
end tell
Thank you, this looks like a very interesting approach. And, any changes needed (I have two systems, a M1 MacBook Pro, and an Intel 2018 Mac mini) to go from one system to the other, and from “Local” to “Remote” backups is very minor.
Since joining MacScripter, and asking a lot of questions, I continue to be amazed at how many ways there are to configure a script to do much the same thing.
Ran into an issue on the first run. I’m sure it’s because I didn’t fully explain how the software (SmartBackup) works. The folders (_changed & _removed) are not created until in the actual process of backing up, there are files that change and/or are removed from the backup. It’s a way of having a backup (some backup software refer to this as an archive) of any file changes or file deletions. So, is there a way to have the script “skip” over any those (_changed & _removed) folders if they do not yet exist?
A possible way is to check if the folder exists before trying to delete the files
property theFolders : {"Documents", "Dropbox Folders", ¬
"Installs", "Movies", "Music", "Photos Archive", "Pictures"}
set basePath to "MacBook Pro Local Backups:SmartBackup Local Backups:Local "
set threeMonthsAgo to (current date) - 90 * days
tell application "System Events"
repeat with i in theFolders
set changedFolder to basePath & i & ":__changed:"
if exists folder changedFolder then
delete (files of folder changedFolder whose creation date < threeMonthsAgo)
end if
set removedFolder to basePath & i & ":__removed:"
if exists folder removedFolder then
delete (files of folder removedFolder whose creation date < threeMonthsAgo)
end if
end repeat
end tell
I don’t think I need to tell you, as I’m sure you already know, the script with the “if exists” works perfectly. This post started out as a question about using “delete” in a script, and ended up allowing me to have a very short, concise, easily modifiable script that does exactly what I need it to do. Thank you!
As “slick” and “clever” as I tend to think that I am at writing AppleScript code, every now and then, someone pops up and takes me right back to school LOL. Good job… Kudos!
I am relatively new to AppleScript. Retired EE who was bored to death with doing nothing, so I joined here to try to pass the time in some meaningful way, as in learning something. You’d be surprised how reading through the various postings helps me understand more and more of how scripts need to be assembled and tested. So, you being taken “back to school” has helped me. Thanks as well.