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

时间:2017-02-07 20:47:38

标签: if-statement applescript

我对applecript很新。我试图根据if条件更改变量的设置。用户选择一个时间,并根据他们选择的时间,变量' time'变化。我收到了错误"行尾不能追到这个“"”。"引用" 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

2 个答案:

答案 0 :(得分:0)

有很多问题:

  • time是保留字。不要将它用作变量。
  • set ... equal to语法错误,您必须撰写set ... to
  • answerchoose from list
  • 的结果无关
  • 即使解决了前三个问题,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 :(得分:0)

1)你不应该使用“时间”作为变量名。它是Applescript中的保留字。例如,选择“myTime”作为变量名。

2)您的脚本中未定义变量“answer”。 «从列表中选择»的结果是默认变量“返回的文本”。如果用户单击取消按钮而不是在列表中选择,则此变量也可以返回“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