If statement 在if/else语句中设置变量?

If statement 在if/else语句中设置变量?,if-statement,applescript,If Statement,Applescript,我对applescript是个新手。我试图根据if条件更改变量的设置。用户选择一个时间,根据他们选择的时间,“时间”变量会发生变化。我得到错误“行尾不能在这个“”之后”,指的是“0”后面的引号,但我需要将这些数字设置为字符串值。我不确定我在这里缺少什么,因此非常感谢您的帮助 property time : "12" choose from list {"12 am", "1 am", "2 am", "3 am", "4 am", "5 am", "6 am", "7 am", "8 am",

我对applescript是个新手。我试图根据if条件更改变量的设置。用户选择一个时间,根据他们选择的时间,“时间”变量会发生变化。我得到错误“行尾不能在这个“”之后”,指的是“0”后面的引号,但我需要将这些数字设置为字符串值。我不确定我在这里缺少什么,因此非常感谢您的帮助

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
有许多问题:

  • time
    是保留字。不要将其用作变量
  • set…等于
    是错误的语法,您必须将
    set…写入
  • 答案
    从列表中选择的结果不相关
  • 即使前三个问题得到解决,
    choose from 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
    
1)不应使用“时间”作为变量名。它是Applescript中的保留字。例如,选择“myTime”作为变量名

2) 脚本中未定义变量“answer”。«从列表中选择»的结果在默认变量“text returned”中。如果用户单击取消按钮而不是在列表中选择,此变量也可能返回“false”。为了脚本的清晰,最好正式分配一个变量

然后脚本变成:

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