Macos AppleScript密码检查
我试图通过AppleScript获得简单的用户身份验证。目标是让密码相互检查,然后继续脚本的其余部分。现在,如果password_initial正确且password_final不正确,脚本可以识别不匹配的密码,但如果password_initial不正确且password_final正确,则没有逻辑来检查自身Macos AppleScript密码检查,macos,applescript,Macos,Applescript,我试图通过AppleScript获得简单的用户身份验证。目标是让密码相互检查,然后继续脚本的其余部分。现在,如果password_initial正确且password_final不正确,脚本可以识别不匹配的密码,但如果password_initial不正确且password_final正确,则没有逻辑来检查自身 set user_name to "" display dialog "Please enter your user name" default answer "firstname.la
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
有人能给我指出正确的方向吗?谢谢 像这样的技巧就是使用 这些代码可以被调用,以便在脚本中运行任意次数,并在重新编写相同代码时保存 它们还可以帮助您避免像现在这样使用重复循环。此外,您还应始终将“取消”按钮添加到显示对话框中。如果重复循环或处理程序调用中存在错误逻辑,则用户需要一种退出方法 我还使一些显示对话框文本动态。用了一些 我已经测试了这段代码,它有句柄和对它们的调用,并且在我的测试中运行良好。但这是一个例子,应该给你足够的时间向前迈进
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
哇,哇,处理程序和全局变量?不需要所有这些,为什么不把整个事情都抛到一个循环中,然后在我们得到想要的东西时打破它呢
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...
因为在我看来,这是一种更好的编写代码的方法。一个原因是使用处理程序意味着我可以更轻松地更改scipt中的逻辑或单个处理程序中的代码,而无需涉及任何其他基本代码或逻辑。处理程序实际上是自包含的,这意味着我可以轻松地将它们复制到其他脚本,而不必像在您的示例中那样分解这些代码。此外,我认为最好尽早了解处理程序,并养成使用它们的习惯。+1,我同意你的观点,处理程序的编码肯定更好(虽然对全局变量不是很确定),如果我要编写一些千行程序,我会在一瞬间使用你的代码而不是我的代码,但对于这么一小段代码来说,感觉您太复杂了。我只花了几分钟就完成了。:-)我可以把它放在更少的地方。但感觉有些功能应该被分解。我不认为代码行数应该决定您如何编写代码。我不会说我不会在没有处理程序的情况下编写代码,我经常这样做。但当我这样做的时候,是因为我认为我在节省时间,而事实并非如此。当我以后想修改代码时,总是后悔。我真的很喜欢这两个答案。我可能会使用循环,因为它的代码更少,但对于更复杂的情况,使用处理程序是一个好主意。谢谢你们的讨论!处理程序确实在很大程度上简化了事情,有时确实更多的代码可以使程序变得更简单,但是对于快速脚本(尤其是在像这样的小AppleScript中),当您可以在单个循环中完成相同的事情时,引用多个方法会让您觉得代码太多了。