【问题标题】:"end if"/"else if" in AppleScript not working?AppleScript 中的“end if”/“else if”不起作用?
【发布时间】:2014-03-05 02:43:23
【问题描述】:

我正在尝试使用具有多个按钮的对话框编写 AppleScript,每个按钮执行不同的命令。我的问题是 AppleScript 编辑器根据标题将“end if”或“else if”检测为语法错误。

例如:

set dialog to display dialog "Test" buttons {"1","2"}
set pressed to button returned of dialog
if pressed is equal to "1" then activate "Safari"
else if pressed is equal to "2" then beep
end if

AppleScript 编辑器显示错误“语法错误:预期行尾等,但发现“else if”。”

是不是我做错了什么?

【问题讨论】:

    标签: macos applescript


    【解决方案1】:

    AppleScript 的条件句有两种格式,一种简单的单行格式:

    if condition then statement
    

    和多行格式:

    if condition then
        statement
    end if
    

    如果你想使用多个语句或有多个条件(else if),你必须使用多行格式:

    set dialog to display dialog "Test" buttons {"1", "2"}
    set pressed to button returned of dialog
    if pressed is equal to "1" then
        activate "Safari"
    else if pressed is equal to "2" then
        beep
    end if
    

    【讨论】:

    • 如果在 else 块上你能结束吗?
    • @fungusanthrax 您必须end if 结束else 块。 else 仅在多行格式中可用,并且始终以 end if 结尾。 (请注意,您可以拥有任意数量的else ifs。)
    • 非常有帮助。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-21
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2023-03-13
    相关资源
    最近更新 更多