Delete folders older than 7 days but ignore some folders

I am new to apple script and am having some issues making this work. I want to be able to delete all files and folders in a certain location older than 7 days. However I would like to ignore some of the folders and not have them deleted. This is the code I have so far but it is not working:

--this happens when you doubble click the droplet
on run
	set aFolder to choose folder
	processOneFolder(aFolder)
end run

--this happens when you drop folder on droplett
--note that we only work with the first dropped item
on open pathoffolder
	processOneFolder(item 1 of pathoffolders)
end open

on processOneFolder(theFolder)
	set folderNamesToIgnore to {"Miscellaneous"}
	
	tell application "Finder"
		repeat with oneFolder in (get folders of theFolder)
			set thisFolderName to name of oneFolder
			if thisFolderName is not in folderNamesToIgnore then
				if (modification date is less than ((get current date) - 7 * days)) then delete oneFolder
			end if
		end repeat
	end tell
end processOneFolder

Any help would be appreciated.
Thanks in advance.

Model: iMac
AppleScript: 2.1.2
Browser: Safari 534.58.2
Operating System: Mac OS X (10.6)

Hello.

Your main problem was that you can’t really access a scripting addition command in the middle of a Finder command, or tell block, not current date anyway, which you by the way can’t get either.

I rewrote your handler slightly, and tested it, and it worked for me. :slight_smile:

on run
	set aFolder to choose folder
	processOneFolder(aFolder)
end run

--this happens when you drop folder on droplett
--note that we only work with the first dropped item
on open pathoffolder
	processOneFolder(item 1 of pathoffolders)
end open

on processOneFolder(theFolder)
	set folderNamesToIgnore to {"Miscellaneous"}
	set treshold to ((current date) - 7 * days)
	tell application "Finder"
		repeat with oneFolder in (get folders of theFolder)
			set thisfoldername to name of oneFolder
			if thisfoldername is not in folderNamesToIgnore then
				if (modification date of oneFolder) is less than treshold then delete oneFolder
			end if
		end repeat
	end tell
end processOneFolder

That fixed it on my end as well. Thank you for the information about the handler. :slight_smile: