Multiple Operators

I’n writing a script to go through the contents of a folder and delete all files and folders except for pre determined folder and files. Currently the script deletes all contents of the folder.

This is what I have:


--Folder and files in parentDirectory to be protected from deletion.
set safeFile01 to "template_01.aep"
set safeFile02 to "template_02.aep"
set safeFolder to "(Footage)"

--Delete all projects, files, folders that are not original project template (watch folder).
		set folderItems to (items of folder projectFolder whose name does not contain safeFile01 & safeFile02 & safeFolder)
		repeat with i from 1 to (count of folderItems)
			if (kind of (item i of folderItems) is "folder") then
				delete folder (item i of folderItems as string)
			else
				delete file (item i of folderItems as string)
			end if
			
		end repeat

I’ve since tried this: Still deletes all contents of folder.


--Delete all projects, files, folders that are not original project templates (watch folder).
		set folderItems to (items of folder projectFolder)
		repeat with i from 1 to (count of folderItems)
			if (kind of (item i of folderItems) is "folder") then
				if ((name of item i) is not equal to safeFolder) then
					delete folder (item i of folderItems as string)
				end if
			else if ((name of item i) is not equal to safeFile01 or safeFile02) then
				delete file (item i of folderItems as string)
			end if
		end repeat

Hi. In the first example, you used “&”, which is the concatenation operator. You were actually looking for a string of all the protected items’ names. Multiples can be tested against in a list format.

set safeFile01 to "template_01.aep"
set safeFile02 to "template_02.aep"
set safeFolder to "(Footage)"

tell application "Finder" to delete (folder "projectFolder"'s items whose name is not in {safeFile01, safeFile02, safeFolder})

Marc beat me to it but here is mine

set projectFolder to choose folder
tell application "Finder" to set folderItems to (items of projectFolder)
set safeItems to {"template_01.aep", "template_02.aep", "(Footage)"} -- array of files and folder to avoid
repeat with anItem in folderItems
       --check to see if it is in the array
	if name of anItem is not in safeItems then 
		delete anItem -- just delete it no need to check if its a file or folder unless you want to look at the files in a folder in which case you'll need to do something different
	end if
end repeat

Just as an aside:

Sometimes it’s handy to have a little truth handler for quick tests. Something quick like this can give you a hint as to whether your logic is working or not.

tell application "Finder" to (display dialog "" & (name of (choose file) contains "trap") giving up after 1)

Cheers!

Jim Neumann
BLUEFROG

If the idea’s to preserve items whose names contain those strings, the syntax would be:


--Folder and files in parentDirectory to be protected from deletion.
set safeFile01 to "template_01.aep"
set safeFile02 to "template_02.aep"
set safeFolder to "(Footage)"

--Delete all projects, files, folders that are not original project template (watch folder).
tell application "Finder"
	delete (items of folder projectFolder whose name does not contain safeFile01 and name does not contain safeFile02 and name does not contain safeFolder)
end tell

Thank you all for the help! :slight_smile:

It works perfectly now. I ended up using a combination of Marc’s and Nigel’s script:


tell application "Finder" to delete (items of folder projectFolder items whose name is not in {safeFile01, safeFile02, safeFolder})

Thank you for the advice Jim. I would love to implement some sort of logic test! I’m not sure I really understand what the truth handler is or how to use it though. Are you simply illustrating that I should put in display dialogs to get info as the script is running? I guess I’m not understanding the “trap” and the “giving up after” syntax.

It’s just a little snippet to test a condition. It can be any condition you’d like. In my example I’m looking for the word “trap” in a filename. My query is based on assuming the “name contains” the value - and I may be wrong. So I’m just asking it to tell me if it’s true or false (the dialog is just a nicety - you can see the results in Script Editor too.)

It doesn’t show you where your logic is wrong but it can be a quick way to see something may be failing. (You could check out Try - On error Blocks too)

Hope that rambling answer made some sense.

Jim

PS: I use display dialogs all the time as a quick way to check values. (And yes there are other tools but I find this the easiest in most cases.)

Thanks Jim-

That makes better sense. Does the “giving up after 1” tell the script to stop once it finds the case “trap”?

Nope. “giving up after” allows a dialog to automatically dismiss itself after n seconds. This is useful for being lazy and for popping up messages that a User doesn’t have to read. :stuck_out_tongue:

Awesome! I can use that.

Does that supersede the timeout? I have a popup that displays a message and if you don’t click on it after n seconds it times out the script. If I use the “giving up after” with say 1800 or (30 * 60) will it in essence continue “not giving up” for that amount of time?

I’m not totally sure about your question (little brain-addled today :P) but.

“With timeout” (in my use and understanding) is a safety valve for your script. It allows you to specify an amount of time for a script to run before it gracefully exits. I actually rarely use “With timeout” so you may want to read up on it, it’s finicky. Here’s a quick link. http://oreilly.com/catalog/aplscptian/chapter/ch07.html

“Giving up after” is exclusively for controlling dialog box behavior. This is a simple method of not letting a dialog with important but not critical info hold up a process.


display dialog "Don't forget to brush and floss today!" giving up after 1

Important but not critical that the User read this (though that may depend on their mouth hygiene :lol:) and no need to have them respond to it.


display dialog "Do NOT press a button unless the aliens HAVE landed!!" buttons {"Run","Hide","Worship"} default button 3

Here it is critical that the process gets held up and the User responds to the dialog box (though the default button may be dangerous in this case! :P). Here you could use a “with timeout” to have the script exit after a maximum period of time but that’s dependent on your needs.

Hope this helps. Cheers!
Jim

Yes, thanks Jim, that makes better sense to me.

I was having trouble with a dialog window that displayed “process complete” to the user. It was critical that the user read the message but I found that if they didn’t see it within (n) minutes the script/application would time out. I used “with timeout” to extend the time out, giving the user more time to see it.

Thanks again,
Shawn :slight_smile: