Reputation: 67
I'm pretty new to applescript. I'm trying to change what a variable is set to based on an if condition. The user chooses a time and depending on what time they choose, the variable 'time' changes. I get the error "A end of line can’t go after this “"”." referring to the quotations following "0", but I need these numbers to be set as string values. Not sure what I'm missing here so any help is appreciated.
property time : "12"
choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
if answer is equal to "12 am" then
set time equal to "0"
else if answer is equal to "1 am" then
set time equal to "1"
end if
Upvotes: 0
Views: 974
Reputation: 3105
1) you should not use "time" as variable name. it is a reserved word in Applescript. for instance, choose "myTime" as variable name.
2) the variable "answer" is not defined in your script. The result of the « choose from list » is in the default variable "text returned". This variable can also return "false" if user clicks on the cancel button instead of choosing in the list. for clarity of the script, better to formally assign a variable
Then script becomes :
set myResponse to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
set UserChoice to item 1 of myResponse
set myTime to "" -- goods practice to initialise a variable
if UserChoice is "12 am" then
set myTime to "0"
else if UserChoice is "1 am" then
set myTime to "1"
end if
log myTime
Upvotes: 0
Reputation: 285260
There are many issues:
time
is a reserved word. Don't use it as a variable.set ... equal to
is wrong syntax, you have to write set ... to
.answer
is not related to the result of choose from list
.And even if the first three issues are resolved, choose from list
returns a list.
property myTime : "12"
set answer to choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am", "9 am", "10 am", "11 am", "12 pm", "1 pm", "2 pm", "3 pm", "4 pm", "5 pm", "6 pm", "7 pm", "8 pm", "9 pm", "10 pm", "11 pm"} with title "Time Selection" with prompt "What time would you like?" OK button name "This Time" cancel button name "Cancel" default items {"12 am"}
if answer is false then return -- catch if nothing is selected
set answer to item 1 of answer -- flatten the list
if answer is equal to "12 am" then
set myTime to "0"
else if answer is equal to "1 am" then
set myTime to "1"
end if
Upvotes: 1