Question About "Delete" in a Script

Ran into an issue this morning. I think I already stated that I have two Mac systems. The M1 MacBook Pro and an Intel 2018 Mac mini. So, I changed the “if exists” script to my Mac mini, and the remote backups as follows:

property theFolders : {"Documents", "Dropbox Folders", ¬
	"Installs", "Movies", "Music", "Photos Archive", "Pictures"}

set basePath to "Mac Mini Remote Backups:SmartBackup Remote Backups:Remote "

set threeMonthsAgo to (current date) - 180 * 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 < sixMonthsAgo)
		end if
		set removedFolder to basePath & i & ":__removed:"
		if exists folder removedFolder then
			delete (files of folder removedFolder whose creation date < sixMonthsAgo)
		end if
	end repeat
end tell

This change involved changing the days from 90 to 180 and the wording from “threeMonthsAgo” to “sixMonthsAgo”.

I get the following error (see screenshot).
Screenshot

Either change this to “sixMonthsAgo” or change the cases where you use it to “threeMonthsAgo”

Can you hear that thumping, that’s me banging my head on the desk. What a dumb mistake. Thank you.

1 Like

I have an Rsync routine that I developed some time ago that works incredibly well. Please see the screenshot for the folder structure. The “if exists” part is not needed as I create the folders when I first set up the backup, so they all the folders are there.

All I’m looking to do is purge any files in the “Archive” folders that are over 90 days old.

I must have tried at least 15 iterations of your script in post (13) here, and still can not get it to work. I’m sure the errors are typical beginners errors. Any chance you could (yet again) give me a hand?

what is the full path to your “Local” Folder?

I created a folder on my Desktop named “Test”. Inside of that “Test” folder I created the folders that you listed in your snapshot above.

This worked for me.

set baseFolder to (path to home folder as text) & "Desktop:Test:"

tell application "System Events"
	set theFolderNames to name of folders of folder baseFolder whose name ends with "Archive"
end tell

set threeMonthsAgo to (current date) - 90 * days

tell application "System Events"
	repeat with i in theFolderNames
		delete (files of folder (baseFolder & i) whose ¬
			creation date < threeMonthsAgo)
	end repeat
end tell

MacBook Pro Local Backups/Sync Folders Local Backups/ (then all folders listed in screenshot)

Don’t understand this part:

set baseFolder to (path to home folder as text)

Although this seems to works:

property theFolders : {"MacBook Pro Local Backups:Sync Folders Local Backups:Local Documents Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Dropbox Folders Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Installs Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Movies Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Music Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Photos Archive Archive:", ¬
	"MacBook Pro Local Backups:Sync Folders Local Backups:Local Pictures Archive:"}

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

This is the relative HFS path to your home folder.

Based on my script to consider all folders inside MacBook Pro Local Backups:Sync Folders Local Backups: try this

set baseFolder to "MacBook Pro Local Backups:Sync Folders Local Backups:"

set threeMonthsAgo to (current date) - 90 * days
tell application "System Events"
	repeat with i in (get folders of folder baseFolder whose name does not start with ".")
		set changedFolder to (path of i) & "__changed:"
		delete (files of folder changedFolder whose creation date < threeMonthsAgo)
		set removedFolder to (path of i) & "__removed:"
		delete (files of folder removedFolder whose creation date < threeMonthsAgo)
	end repeat
end tell

It’s also necessary to filter the invisible files because unlike the Finder System Events can “see” always the invisible files (those starting with a dot)

So, I’ll need to add the part about the “.” to the one I’m already using from your post 13. And, in this case there are no “_changed” or “_removed” folders. For this backup I only created an “Archive” folder, into which rsync puts copies of all changed or deleted files.

Example:
MacBook Pro Local Backups/Sync Folders Local Backups/Local Documents
MacBook Pro Local Backups/Sync Folders Local Backups/Local Documents Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Dropbox Folders
MacBook Pro Local Backups/Sync Folders Local Backups/Local Dropbox Folders Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Installs
MacBook Pro Local Backups/Sync Folders Local Backups/Local Installs Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Movies
MacBook Pro Local Backups/Sync Folders Local Backups/Local Movies Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Music
MacBook Pro Local Backups/Sync Folders Local Backups/Local Music Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Photos Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Photos Archive Archive
MacBook Pro Local Backups/Sync Folders Local Backups/Local Pictures
MacBook Pro Local Backups/Sync Folders Local Backups/Local Pictures Archive

So I came up with this for the SmartBackup script:

set baseFolder to "MacBook Pro Local Backups:SmartBackup Local Backups:"

set threeMonthsAgo to (current date) - 90 * days
tell application "System Events"
	repeat with i in (get folders of folder baseFolder whose name does not start with ".")
		set changedFolder to (path of i) & "__changed:"
		delete (files of folder changedFolder whose creation date < threeMonthsAgo)
		set removedFolder to (path of i) & "__removed:"
		delete (files of folder removedFolder whose creation date < threeMonthsAgo)
	end repeat
end tell

And this for the Sync Folders script:

set baseFolder to "MacBook Pro Local Backups:Sync Folders Local Backups:"

set threeMonthsAgo to (current date) - 90 * days
tell application "System Events"
	repeat with i in (get folders of folder baseFolder whose name does not start with ".")
		set archiveFolder to (path of i) & "Archive:"
		delete (files of folder archiveFolder whose creation date < threeMonthsAgo)
	end repeat
end tell

That should work, thanks to your help.

I like this approach. It doesn’t force you to empty the recycle bin when the requirement is really just to permanently delete the files that are created more than 90 days ago.

It’s a double edged sword though… while using System Events to delete files, is yes a one step process, that action is permanent. There is no way to “un-delete” those files if you made a mistake. This could turn out to be a disaster, especially while testing out the code to see if it works. One can easily delete the wrong files. However, if you code Finder to delete the files, then after the fact you realize your code deleted the wrong files… Those deleted files can be restored back to their original locations from the Trash bin.

I was at the Boston Computer Society meeting on January 30, 1984, when Steve Jobs introduced the Macintosh. Bought my first Mac in 1985. So, I go back to the days when HD failure wasn’t if, but when, and I tend to be a bit fanatical about backups. I run three different commercial backup software as well as my own rsync routine and Apple’s Time Machine. All my local backups delete changed/deleted files after 90 days, my remote backups delete those files after 180 days. My thought is that if I haven’t needed a missing file for that 180 days, it probably wasn’t that important to begin with. I’ve enjoyed (as well as learned a few things) this latest discussion regarding scripts to automatically delete those files. And being retired, what more could I ask.

Fair enough. I liked that the emptying of the recycle bin is not forced on you as a side effect of another requirement.

I have a different strategy for my “expiring” files. I perform the delete daily scheduled automatically in the morning. I have configured my apps/habit to put transient files in it directly. For example, download/screenshot files. It forces me to put important files in their right (organized) places and at the same time I don’t keep clutter longer than one day.