2014-01-17 91 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