Deletig specific files

Hi,
I am a total noob to AppleScript and cannot find a simple reference for the syntax.

What I would like to do is delete files based on extension and some folders based on their name.

Basically I want a folder action running on a folder, the script will look at all the files dropped in and:

delete *.txt
delete *.jpg

remove any folder that has “test” in it’s name.

I have this script that I hacked together, this deletes all files that get put in, I cannot figure out how to write conditionals though.

Thanks.

Phil


on adding folder items to thisFolder after receiving addedItems
	
	repeat with movieFile in addedItems
		
		tell application "Finder"
			delete (files of movieFile)
			delete folder movieFile
			
		end tell
		
	end repeat
	
end adding folder items to


You have a list of added files called addedItems. You have to use a repeat loop to cycle through the list and check each list item individually. So we can make one if statement with all your conditions, and if one of the conditions is met we delete that file. That’s how you handle a list.

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		repeat with anItem in addedItems
			if (name extension of anItem is "txt") or (name extension of anItem is "jpg") or (class of anItem is folder and name contains "test") then
				delete anItem
			end if
		end repeat
	end tell
end adding folder items to

On the other hand, you can use a “whose filter” to filter while the Finder is checking the contents of a folder. So something like this would work if you always wanted to check all the files in “thisFolder” rather than just the addedItems.

on adding folder items to thisFolder after receiving addedItems
	tell application "Finder"
		set foundItems to items of thisFolder whose ((name extension of anItem is "txt") or (name extension of anItem is "jpg") or (class of anItem is folder and name contains "test"))
		delete foundItems
	end tell
end adding folder items to

I hope that helps.

Thanks for the help, I tried both scripts.

The 2nd one didn’t seem to do anything oddly enough.

The first one deleted the files but not the folder named “test” (the folder still had files in it, is that an issue?).

Also, I need to figure out how to get the folder action to look inside a sub folder because that’s how the files are dropped.

Any ideas?

Thanks.

Phil