简体   繁体   English

AppleScript中的“如果结束” /“否则如果结束”不起作用?

[英]“end if”/“else if” in AppleScript not working?

I am trying to code an AppleScript with a dialog box that has multiple buttons, which each execute different commands. 我正在尝试使用具有多个按钮的对话框编写AppleScript的代码,每个按钮执行不同的命令。 My problem is that AppleScript Editor detects either "end if" or "else if" as a syntax error, as per the title. 我的问题是,AppleScript编辑器根据标题检测到“ end if”或“ else if”作为语法错误。

For example: 例如:

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 Editor displays the error " Syntax Error : Expected end of line, etc. but found “else if”." AppleScript编辑器显示错误“ 语法错误 :预期的行尾等,但找到”否则”。

Is there something I'm doing wrong? 我做错了什么吗?

AppleScript's conditionals have two formats, a simple single-line format: AppleScript的条件格式有两种格式,一种是简单的单行格式:

if condition then statement

and a multiline format: 和多行格式:

if condition then
    statement
end if

If you want to use multiple statements or have multiple conditions ( else if ), you must use the multiline format: 如果要使用多个语句或有多个条件( 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM