我正在尝试通过AppleScript进行简单的用户身份验证。目标是让密码相互检查,然后继续使用脚本的其余部分。现在,如果password_initial正确且password_final不正确,脚本可以识别不匹配的密码,但如果password_initial不正确且password_final正确,则没有逻辑可以自行检查。
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
有人能指出我正确的方向吗?谢谢!
答案 0 :(得分:1)
这样的诀窍就是使用Handlers。
这些代码可以被调用以在您的脚本中运行多次,并且可以再次重写相同的代码。
它们还可以帮助您不像您一样使用重复循环。此外,您应始终在显示对话框中添加“取消”按钮。如果重复循环或处理程序调用中存在错误的逻辑,则用户需要一种方法来退出它。
我还制作了一些动态的显示对话框文本。并使用了一些global variables
我已经测试过这个代码,它有Handles和对它们的调用,它在我的测试中运行良好。但这是一个例子,应该足以让你前进。
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
答案 1 :(得分: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...