Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Tell me whats wrong with this script

I am trying to check the first part of a file name and if it doesn't have the right begginning then I want to move it to another folder. The problem is that its not checking past the first "RepCode" in the list

tell application "Finder"
set FileName to item 1 in folder "OutFolder:Out" as text
set RepCode to items 19 thru 21 in FileName as text
try
if RepCode is not "A10" or "A11" or "A12" or "A13" or "A14" or "A15" or "A16" or "A20" or "A22" or "A23" or "A26" or "A30" or "C33" or "C34" or "C40" or "C41" or "C42" or "C44" then move FileName to folder "OutFolder::No Rep Code"
end try
end tell

why wont this work?

Posted on Oct 17, 2008 1:35 PM

Reply
Question marked as Best reply

Posted on Oct 17, 2008 1:45 PM

Use the following:

if RepCode is not in {"A10", "A11", "A12", "A13", "A14", "A15", "A16", "A20", "A22", "A23", "A26", "A30", "C33", "C34", "C40", "C41", "C42", "C44"} then move FileName to folder "OutFolder:No Rep Code"

(36622)
4 replies

Oct 17, 2008 2:13 PM in response to BenChase

Just so you understand why your original code fails, you need to understand how the 'if' statement works with or conditions.

Each 'or' separates different test conditions. Therefore your statement:

if RepCode is not "A10" or "A11" or "A12" or "A13" then...


(condensed for illustration) can be read as:

if (RepCode is not "A10") or ("A11") or ("A12") or ("A13")...


Each parentheses condition needs to stand alone as a valid true/false condition, so your first check sees if RepCode is not "A10" then it checks if "A11" is true - there's no relationship between RepCode and A11 since they're two separate conditions.

The first way to change this is in the form:

if (RepCode is not "A10") and (RepCode is not "A11") and (RepCode is not "A12") and (RepCode is not "A13")...


but clearly that gets very verbose, hence Neil's suggestion to use a list and see if RepCode is in the list.
Note also the switch to using and rather than or since you want all the conditions to be true (RepCode doesn't match ANY of the values), otherwise it will return true if it matches ANY.

Oct 17, 2008 3:16 PM in response to Camelot

Niel thanks for the code that worked great and thanks to Camelot for explaining, that really helps.

I have another questions about my statement. I want the "then" to do 2 things not just move. This is what I was trying but it doesn't work either.

if RepCode is not in {"A10", "A11", "A12", "A13", "A14", "A15", "A16", "C44"} then set RepCode to NoRepCode and (move FileName to folder "Current Art:Proofs Out:No Rep Code" with replacing)

I want to set it to a different variable and then move the file to another folder while still allowing the correct files to flow thru the rest of the script.

Tell me whats wrong with this script

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.