4 Replies Latest reply: Oct 17, 2008 3:25 PM by Niel
BenChase Level 1 Level 1 (10 points)
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?
  • Niel Level 10 Level 10 (265,465 points)
    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)
  • Camelot Level 8 Level 8 (46,120 points)
    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.
  • BenChase Level 1 Level 1 (10 points)
    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.
  • Niel Level 10 Level 10 (265,465 points)
    Here:

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

    A full if block is needed if you want to execute more than one command.

    (36630)