2

我正在尝试通过 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

谁能指出我正确的方向?谢谢!

4

2 回答 2

1

像这样的技巧是使用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
于 2013-07-23T17:04:10.047 回答
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...
于 2013-07-23T17:13:34.887 回答