像这样的技巧是使用Handlers。
这些是可以调用的代码位,可以在您的脚本中任意多次运行,并且可以节省再次重写相同代码的时间。
它们还可以帮助您不要像正在做的那样使用重复循环。此外,您应该始终将“取消”按钮添加到显示对话框中。如果重复循环或处理程序调用中有错误的逻辑,用户需要一种退出它的方法。
我还制作了一些动态显示对话框文本。并使用了一些全局变量
我已经测试了这段代码,它有句柄和对它们的调用,它在我的测试中运行良好。但这是一个例子,应该给你足够的进步。
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