Search code examples
macospasswordsapplescriptosx-mavericks

Applescript a "Retry" button


I'm coding an application with a password field:

set password1 to text returned of (display dialog "To continue please enter your special passcode below." buttons {"Cancel", "Continue"} default button 2 default answer "" cancel button 1 with hidden answer)
if the password1 is "passwordhere" then
display dialog "You have entered the password right!

    The right password was: passwordhere
    The entered password was: " & password1 buttons {"Cancel", "Continue"} default button 2 cancel button 1
else
if the password1 is "" then
set password1 to "Empty Passwordfield"
end if
display dialog "You have entered the password wrong!

    The right password was: ********
    The entered password was: " & password1 buttons {"Cancel", "Try Again"} default button 2 cancel button 1
end if
end

Now my question is: How do I make a "Try Again" button so it will return to the first display dialog? Is this possible? If not please say that in an answer.

Also a question: Is this also possible for just 2 normal dialogs? So that in the second dialog is a "Back" button? And if you press that button you'll return to the first dialog?

Thanks for reading,

Jort


Solution

  • Consider placing the password prompt/dialog in a applescript handler:

    on get_password()
    -- get password and return true if good, false if bad
    end get_password
    

    This way you can 're-call' the handler from anywhere else in your script as needed.

    Here is an example of your code that repeats the prompt for password as your question asked:

    local goodpassword, password1, tryagain, proceed
    
    set goodpassword to false -- initial set
    set tryagain to true -- initial set
    
    on getpassword()
        return text returned of (display dialog "To continue please enter your special passcode below." buttons {"Cancel", "Continue"} default button 2 default answer "" cancel button 1 with hidden answer)
    end getpassword
    
    
    repeat while tryagain = true
        set proceed to true
        set password1 to getpassword() -- This calls the prompt for the first time
    
        if the password1 is "passwordhere" then
            set goodpassword to true
            set tryagain to false
            try
                display dialog "You have entered the password right!
        The right password was: passwordhere
        The entered password was: " & password1 buttons {"Cancel", "Continue"} default button 2 cancel button 1
    
            on error number -128
                set proceed to false
            end try
    
        else
            set goodpassword to false
            if the password1 is "" then
                set password1 to "Empty Passwordfield"
            end if
            try
                display dialog "You have entered the password wrong!
    
        The right password was: ********
        The entered password was: " & password1 buttons {"Cancel", "Try Again"} default button 2 cancel button 1
            on error number -128
                set tryagain to false
            end try
        end if
    end repeat