2014-01-17 94 views
0

我真的很新AppleScript编码,我想知道这段代码出了什么问题。错误是“变量结果未定义”。为什么发生错误?我的代码如下:变量结果未定义

display dialog "Hello and welcome to my app!" buttons {"Login", "Quit"} default button 1 

if the button returned of the result is "Login" then 
    display dialog "Username:" buttons {"Raphi", "Guest"} default button 1 with title  "Choose user" 
else 
    tell application "My app" to quit 
end if 
if the button returned of the result is "Raphi" then 
display dialog "Password:" default answer "" buttons {"Submit"} with title "Enter password" with hidden answer 
else 
display dialog "You have selected guest! Guest is not currenty enabled, please ask  Raphi to enable it. Thank you!" buttons {"OK"} 
end if 
if the text returned of the result is "123" then 
display dialog "Would you like to continue or exit?" buttons {"Continue", "Exit"} 
else 
display dialog "Incorrect password" buttons {"OK"} default button 1 with icon stop 
end if 
if the button returned of the result is "OK" then 
tell application "My app" to quit 
end if 
if the button returned of the result is "Continue" then 
display dialog "" 
else 
tell application "My app" to quit 
end if 

回答

1

错误是在过去的几年if语句,他们需要用一个else if绑在一起,以获得正确的结构赶3种可能的按钮选择。

另一个调整是改变tell application "My app" to quit,至return。这将退出脚本的执行,将退出它(注:如果您保存为一个应用程序,将其设置为保持开放

这同时测试,它没有退出脚本编辑器也会帮助每次运行脚本。或者,您可以用quit简化该块,但我认为return更清洁。

请参见下面的修改:

display dialog "Hello and welcome to my app!" buttons {"Login", "Quit"} default button 1 

if the button returned of the result is "Login" then 
    display dialog "Username:" buttons {"Raphi", "Guest"} default button 1 with title "Choose user" 
else 
    return 
end if 

if the button returned of the result is "Raphi" then 
    display dialog "Password:" default answer "" buttons {"Submit"} with title "Enter password" with hidden answer 
else 
    display dialog "You have selected guest! Guest is not currenty enabled, please ask  Raphi to enable it. Thank you!" buttons {"OK"} 
    return 
end if 

if the text returned of the result is "123" then 
    display dialog "Would you like to continue or exit?" buttons {"Continue", "Exit"} 
else 
    display dialog "Incorrect password" buttons {"OK"} default button 1 with icon stop 
end if 

if the button returned of the result is "OK" then 
    return 
else if the button returned of the result is "Continue" then 
    display dialog "" 
else 
    return 
end if 
0

试试这个:

set dialogResult to display dialog ... 

然后检查返回值:

if button returned of dialogResult is ... then