AppleScript 中的“end if”/“else if”不起作用?

ebo*_*ue3 2 macos applescript

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

例如:

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
Run Code Online (Sandbox Code Playgroud)

AppleScript 编辑器显示错误“语法错误:预期的行尾等,但发现“否则如果”。

有什么我做错了吗?

Gor*_*son 6

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

if condition then statement
Run Code Online (Sandbox Code Playgroud)

和多行格式:

if condition then
    statement
end if
Run Code Online (Sandbox Code Playgroud)

如果要使用多个语句或具有多个条件 ( 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
Run Code Online (Sandbox Code Playgroud)