像這樣的東西的技巧是使用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
因爲IMO是編寫代碼的好方法。一個原因是使用處理程序意味着我可以更輕鬆地更改單個處理程序中的scipt中的邏輯或代碼,而無需觸及任何其他基本代碼或邏輯。處理程序實際上是自包含的,這意味着我可以輕鬆地將它們複製到其他腳本中,而無需像在您的示例中那樣分解這些代碼和代碼。另外我認爲最好早點了解Handlers並養成使用它們的習慣。 – markhunte
+1,我同意你的說法處理程序肯定是更好的編碼(不太確定的全局變量雖然),如果我打算做一些千行程序,我會心跳使用你的代碼過我,但對於這樣一小段代碼讓人覺得你太過於複雜。 – scohe001
它只花了我幾分鐘時間寫出來。 :-)我可以保留在較少的處理程序中。但是覺得應該分解一些功能。我覺得代碼行數不應該決定你編寫代碼的方式。我不會說我不會在沒有處理程序的情況下編寫代碼,我會花很多時間。但是當我這樣做時,是因爲我認爲我節省了時間,而不是。當我想要進行更改時,稍後再轉到代碼時,總是感到後悔。 – markhunte