2013-07-23 88 views
2

我試圖通過AppleScript獲取簡單的用戶身份驗證。目標是讓密碼互相檢查,然後繼續處理腳本的其餘部分。如果password_initial正確並且password_final不正確,那麼腳本可以識別錯誤匹配的密碼,但是如果password_initial不正確並且password_final正確,則沒有邏輯來檢查自身。AppleScript密碼檢查

set user_name to "" 
display dialog "Please enter your user name" default answer "firstname.lastname" 
set the user_name to the text returned of result 

set password_initial to "" 
display dialog "Please enter your password:" default answer "" with hidden answer 
set password_initial to the text returned of result 

display dialog "Please verify your password below." buttons {"OK"} default answer "" with hidden answer 
set password_final to text returned of result 
considering case 
    repeat while password_final is not equal to password_initial 
     display dialog "Passwords do not match, please re-enter your password below." buttons {"OK"} default answer "" with hidden answer 
     set password_final to text returned of result 
    end repeat 
end considering 

--do something 

任何人都可以在正確的方向指向我嗎?謝謝!

回答

1

哇哇哇,處理程序和全局變量?不需要所有這些,爲什麼不把所有的東西放到一個循環中,然後當我們得到我們想要的東西時將其分解?

set user_name to text returned of (display dialog "Please enter your user name" default answer "firstname.lastname") 

set display_text to "Please enter your password:" 
repeat 
    considering case 
     set init_pass to text returned of (display dialog display_text default answer "" with hidden answer) 
     set final_pass to text returned of (display dialog "Please verify your password below." buttons {"OK"} default button 1 default answer "" with hidden answer) 
     if (final_pass = init_pass) then 
      exit repeat 
     else 
      set display_text to "Mismatching passwords, please try again" 
     end if 
    end considering 
end repeat 

#Rest of script here... 
+1

因爲IMO是編寫代碼的好方法。一個原因是使用處理程序意味着我可以更輕鬆地更改單個處理程序中的scipt中的邏輯或代碼,而無需觸及任何其他基本代碼或邏輯。處理程序實際上是自包含的,這意味着我可以輕鬆地將它們複製到其他腳本中,而無需像在您的示例中那樣分解這些代碼和代碼。另外我認爲最好早點了解Handlers並養成使用它們的習慣。 – markhunte

+0

+1,我同意你的說法處理程序肯定是更好的編碼(不太確定的全局變量雖然),如果我打算做一些千行程序,我會心跳使用你的代碼過我,但對於這樣一小段代碼讓人覺得你太過於複雜。 – scohe001

+1

它只花了我幾分鐘時間寫出來。 :-)我可以保留在較少的處理程序中。但是覺得應該分解一些功能。我覺得代碼行數不應該決定你編寫代碼的方式。我不會說我不會在沒有處理程序的情況下編寫代碼,我會花很多時間。但是當我這樣做時,是因爲我認爲我節省了時間,而不是。當我想要進行更改時,稍後再轉到代碼時,總是感到後悔。 – markhunte

1

像這樣的東西的技巧是使用Handlers

這些代碼位可以稱爲你希望你的腳本中運行多次,節省了再次重寫相同的代碼。

他們還可以幫您不要使用重複循環,就像你在做什麼。你也應該總是添加'取消'按鈕到你的顯示對話框。如果重複循環或處理程序調用中存在錯誤的邏輯,用戶需要一種方法來退出它。

我也做了一些顯示對話框文本動態的。並使用了一些global variables

我已經測試此代碼具有手柄和他們的電話和它運作良好,在我的測試。但它是一個例子,應該給你足夠的前進。

set displays to {"Please enter your password:", "Please verify your password below.", "Passwords do not match, please re-enter your password below."} 

    set user_name to add_user_name() #get user name 

    set thedisplay to item 1 of displays #set the fisrt dialog for the password display to the first item in displays 
    global thedisplay, displays, password_initial #global variables 

    set password_final to setDetails() #Call to start the password dialogs 

--your code here .. 

    #HANDLERS 
    on setDetails() 

     set password_initial to add_password() #call to get user password 

     set password_final to verify_password() #call to get verify password 
    end setDetails 


    on add_user_name() 
     display dialog "Please enter your user name" buttons {"Cancel", "OK"} default answer "firstname.lastname" 
     set the user_name to the text returned of result 
     return user_name 
    end add_user_name 

    on add_password() 

     display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer 
     set password_initial to the text returned of result 
     return password_initial 
    end add_password 

    on verify_password() 
     set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays 
     display dialog thedisplay buttons {"Cancel", "OK"} default answer "" with hidden answer 
     set password_final to text returned of result 

     considering case 

      if password_final is not equal to password_initial then 
       set thedisplay to item 3 of displays #set the dialog for the password verify display to the third item in displays 
       my setDetails() # start over again asking for password as it did not does not match dialog displays will also change accordingly 
      else 
       set thedisplay to item 2 of displays #set the dialog for the password verify display to the second item in displays 
      end if 
     end considering 
     return password_final 
    end verify_password