我是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
答案 0 :(得分: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
答案 1 :(得分:0)
请改为尝试:
set dialogResult to display dialog ...
然后检查返回值:
if button returned of dialogResult is ... then