Applescript语法错误“预期”,否则“等,但发现脚本结束。”

时间:2013-03-01 23:17:06

标签: syntax applescript

我在AppleScript中遇到了麻烦:

display dialog "Open Which Application" buttons {"Chrome", "AppleScript", "Textedit"} 
set the button_pressed to the button returned of the result
if the button_pressed is "Chrome" then
Open application "Google Chrome"-- action for 1st button goes here
if the button_pressed is "Applescript" then
Open application "Applescript Editor"-- action for 2nd button goes here
if the button_pressed is "Textedit" then
Open application "Textedit"-- action for 3rd button goes here
end

它一直在说SYNTAX错误:预期“其他”等,但发现脚本结束。

我该怎么办

2 个答案:

答案 0 :(得分:0)

您有三个if语句,但只有end个语句。

您可能需要的是else if

display dialog "Open Which Application" buttons {"Chrome", "AppleScript", "Textedit"}
set the button_pressed to the button returned of the result
if the button_pressed is "Chrome" then
    open application "Google Chrome" -- action for 1st button goes here
else if the button_pressed is "Applescript" then
    open application "AppleScript Editor" -- action for 2nd button goes here
else if the button_pressed is "Textedit" then
    open application "TextEdit" -- action for 3rd button goes here
end if

(另外,你真的应该使用end if,而不只是end,但AppleScript编辑器会为你解决这个问题。)

或者,您可以end每个人:

display dialog "Open Which Application" buttons {"Chrome", "AppleScript", "Textedit"}
set the button_pressed to the button returned of the result
if the button_pressed is "Chrome" then
    open application "Google Chrome" -- action for 1st button goes here
end if
if the button_pressed is "Applescript" then
    open application "AppleScript Editor" -- action for 2nd button goes here
end if
if the button_pressed is "Textedit" then
    open application "TextEdit" -- action for 3rd button goes here
end if

但是,这些案件显然是相互排斥的,因此没有理由不使用else if

如果有帮助,请逐个输入行,并查看AppleScript编辑器如何缩进它们:

display dialog "Open Which Application" buttons {"Chrome", "AppleScript", "Textedit"} 
set the button_pressed to the button returned of the result
if the button_pressed is "Chrome" then
    Open application "Google Chrome"-- action for 1st button goes here
    if the button_pressed is "Applescript" then
        Open application "Applescript Editor"-- action for 2nd button goes here
        if the button_pressed is "Textedit" then
            Open application "Textedit"-- action for 3rd button goes here
        end

这应该让人明白什么是错的。

答案 1 :(得分:0)

尝试:

display dialog "Open Which Application" buttons {"Chrome", "AppleScript", "Textedit"}
set the button_pressed to the button returned of the result
if the button_pressed is "Chrome" then tell application "Google Chrome" to activate -- action for 1st button goes here 
if the button_pressed is "Applescript" then tell application "AppleScript Editor" to activate -- action for 2nd button goes here 
if the button_pressed is "Textedit" then tell application "TextEdit" to activate -- action for 3rd button goes here end