Applescript if, else if Syntax Error

I am trying to set up an if, else if statement that will trigger a specific folder to be generated based on a choice from a list. When I try to run the script it provides the following Syntax Error once the script hits the first else if,


Expected "end" or "end tell" but found "else if"


User uploaded file


Not quite sure why it is giving me this error

MacBook Pro with Retina display, OS X Yosemite (10.10.4)

Posted on Sep 10, 2015 10:34 AM

Reply
9 replies

Sep 10, 2015 12:16 PM in response to theDAMspencer

In your script, dir_2 is only defined if ServiceType = "ADV". If ServiceType is any other value, then the line that creates the directory and the dir_2 variable is never executed.


I think you need to think a little deeper about what it is you're trying to do. It seems that all your checks do the same action (make new folder at dir_1...) and just assign it to different variables. Are you using these other variables in any way later in the script? What's your thinking behind dir_2 vs. dir_3 vs. dir_4, etc.?


In addition, now that I look at it, it will never work anyway.


You start your script by defining ServiceType as a list:


set ServiceType to {"ADV", "B2B", "PR", "PRO-BONO", "MEDIA"}


You then check if ServiceType is a particular value:


if (ServiceType = "ADV") then ...


This check will fail 100% of the time. ServiceType is a list and will never match the "ADV" string.

Maybe what you want to compare is selectedService, since this will represent one of the selected items:


set selectedService to (choose from listServiceType)


but then it will still fail because choose from list will always return a list of items (even if only one of the options was selected), so you need something like:


if (selectedService = {"ADV"}) then ...


or:


if (item 1 of selectedService = "ADV") then ...

Sep 10, 2015 12:26 PM in response to Camelot

My ultimate goal is to create a folder structure based on the Service Type that is selected from a list. Otherwise, I have the script creating the folder structure for all service types, which is not needed. After the script is done running I am then going back and deleting the un-necessary service type folder structures.


I will have to play with your suggestion and see how it goes.

Sep 10, 2015 11:15 AM in response to theDAMspencer

Add 'if' to the beginning of the remaining lines. Alternatively, use:


set dir_2 to make new folder at dir_1 with properties {name:ClientNumber & " " & ClientName & " - " & ServiceType}


instead of repeatedly checking the variable's content. If it's done this way, you'll need to replace all instances of dir_3 and above in the lines after that segment.


(133117)

Sep 10, 2015 11:20 AM in response to Niel

Both suggestions seem to be working but now I am getting a new error further down the code that dir_2 is not defined. The funny thing is I didn't get this error until I changed it from creating all folders to incorporating the if statement. When executing the script I am choosing a value other than dir_2. It seems the script doesn't know what to do if the first choice isn't selectd


User uploaded file


Code without the error message on the top of it.

User uploaded file

Sep 10, 2015 2:32 PM in response to theDAMspencer

It sounds like you have a condition - the selected service type - and a number of actions that need to be taken based on its value.


On that basis you should try to break the code into manageable chunks where each chunk performs a particular set of actions (e.g. create a directory and its subdirectories) related to each of the conditions.


At it stands you have multiple chunks that relate around the actions, not around the conditions. For example you have a chunk of code that creates various top-level directories (dir_2, dir_3, dir_4, etc.). You then have a disconnected chunk of code that creates subdirectories within the top-level directory, but you've lost context (you no act based on which ServiceType was selected).


Instead, consider the following:


set ServiceType to {"ADV", "B2B", "PR", "PRO-BONO", "MEDIA"}


set selectedService to (choose from listServiceType)


-- here's what to do if 'ADV' is selected:

if (item 1 of selectedService = "ADV") then

set dir_2 to makenewfolderatdir_1with properties {name:ClientNumber & " " & ClientName & " " & ServiceType}

set sub1_2_1 to make new folder at dir_2 with properties {name:ClientName & " 2015"}

set sub2_2_1 to make new folder at sub1_2_1 with properties {name:ClientName & " 2015 Archive"}

set sub2_2_2 to make new folder at sub1_2_1 with properties {name:ClientName & " 2015 Art Assets"}

set sub3_2_1 to make new folder at sub2_2_2 with properties {name:ClientName & " 2015 Branding"}

end if


-- here's what to do if 'B2B' is selected:

if (item 1 of selectedService = "ADV") then

set dir_3 to makenewfolderatdir_1with properties {name:ClientNumber & " " & ClientName & " " & ServiceType}

set sub1_3_1 to make new folder at dir_3 with properties {name:ClientName & " 2015"}

set sub2_3_1 to make new folder at sub1_3_1 with properties {name:ClientName & " 2015 Archive"}

set sub2_3_2 to make new folder at sub1_3_1 with properties {name:ClientName & " 2015 Foo"}

set sub3_3_1 to make new folder at sub2_3_2 with properties {name:ClientName & " 2015 Other Stuff"}

end if


-- etc.


In this model you setup your variables (the ServiceType list and selection) and then run one of a number of chunks depending on what was selected. This makes it a lot easier to look at the code and predict what actions will be taken for each option in the list.

Sep 12, 2015 3:23 AM in response to theDAMspencer

Hello


if - else if statement in AppleScript is as follows. In this form, "if ... then", "else if ... then" and "else" must be followed by return.



set x to choose from list {"A", "B", "C", "D"} if x = false then error number -128 set x to x's item 1 if x = "A" then set y to 1 else if x = "B" then set y to 2 else if x = "C" then set y to 3 else set y to 0 end if return {x, y}



As for creating folders, mkdir(1) via do shell script command would be much simpler and faster. Something like this. (Directories creation is only partially implemented for the case ServiceType = "ADV", for you're not showing the full details)



set ClientName to text returned of (display dialog "Please enter CLIENT NAME" default answer "CLIENT NAME in all uppercase") set ClientNumber to text returned of (display dialog "Please enter Client Number" default answer "4-Digit Client Number") set ServiceTypes to {"ADV", "B2B", "PR", "PRO-BONO", "MEDIA"} set ServiceType to choose from list ServiceTypes if ServiceType is false then error number -128 -- user cancel set ServiceType to ServiceType's item 1 set loc to (choose folder with prompt "Choose Folder Location")'s POSIX path set yr to "2015" set args to "" repeat with a in {loc, yr, ClientName, ClientNumber, ServiceType} set args to args & a's quoted form & space end repeat if ServiceType = "ADV" then do shell script "/bin/bash -s <<'EOF' - " & args & " # $1 = location, $2 = year, $3 = ClientName, $4 = ClientNumber, $5 = ServiceType cd \"$1\" || exit mkdir -p \"$3/$4 $3 $5/$3 $2/$3 $2 Archive\" mkdir -p \"$3/$4 $3 $5/$3 $2/$3 $2 Art Assets/$3 $2 Branding\" EOF" else if ServiceType = "B2B" then -- omitted else if ServiceType = "PR" then -- omitted else if ServiceType = "PRO-BONO" then -- omitted else if ServiceType = "MEDIA" then -- omitted end if



Hope this may help,

H

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript if, else if Syntax Error

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