【问题标题】:Applescript Syntax Error "Expected “else”, etc. but found end of script."Applescript 语法错误“预期为“else”等,但发现脚本结尾。”
【发布时间】:2013-03-02 07:53:06
【问题描述】:

我在 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 ERROR: Expected “else”, etc. but found end of script.

我该怎么办

【问题讨论】:

    标签: syntax applescript


    【解决方案1】:

    您有三个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 Editor 会为你解决这个问题。)

    或者,您可以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 Editor 是如何缩进的:

    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
    

    这应该很明显出了什么问题。

    【讨论】:

      【解决方案2】:

      试试:

      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
      

      【讨论】:

        猜你喜欢
        • 2016-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多