Search code examples
passwordsapplescriptinputbox

Make Username and Password input box with AppleScript


I would like to make a dialog input box that looks exactly like this in AppleScript:

Except without the picture in the top left of the lock.

Also, I need to be able to save both inputs.

I know I can use tell application "System Events" to display dialog "blah blah" default answer "" end tell but I can not find a way to have multiple and labeled fields.


Solution

  • Natively, AppleScript doesn't offer this capability as of OSX 10.10.

    To see what GUI operations are supported, check the User Interaction suite of the dictionary of the Standard Additions (StandardAdditions.def, accessible from Script Editor.app via File > Open Dictionary... > StandardAdditions.osax).

    The closest approximation is a single input-field dialog - prompting for a password only - as follows (a limitation you've already noted in the question, but just to illustrate how to prompt for a password and how to use custom buttons):

    display dialog ¬
        "Installer is ..." default answer ¬
        "" buttons {"Cancel", "Install Software"} ¬
        default button 2 ¬
        with hidden answer
    

    To get what you want, you need a third-party library such as Pashua.

    Here's how you would define the requested dialog in Pashua and process the returned values:

    # Define the dialog using a textual definition similar to a properties file.
    set dlgDef to "
    # Window title (if you don't set one explicitly (even if empty), it'll be 'Pashua')
    *.title = 
    
    # Add the hint (static text).
    st.type = text
    st.text = Installer is trying to install new software. Type your password to allow this.
    
    # Add the username field.
    tfn.type = textfield
    tfn.label = Name:
    
    # Add the password field.
    tfp.type = password
    tfp.label = Password:
    
    # Add the buttons.
    cb.type = cancelbutton
    db.type = defaultbutton
    db.label = Install Software
    "
    
    # Show the dialog. 
    # Return value is a record whose keys are the element names from the dialog
    # definition (e.g., "tfn" for the  usernam text field) and whose
    # values are the values entered (for input fields) or 
    # whether a given button was clicked or not ("1" or "0")
    set theResult to showDialog(dlgDef, "")
    
    # Process the returned values.
    if cb of theResult is "1" then
        display alert "User canceled the dialog."
    else
        display alert "name=[" & tfn of theResult & "]; password=[" & tfp of theResult & "]"
    end if
    

    The dialog will look like this:

    enter image description here


    Overview of setting up Pashua

    Note: This is a simplified overview; for full instructions, see Read me.html and Documentation.html in the downloaded disc image.

    • Download and mount the disc image from http://www.bluem.net/en/mac/pashua/
    • Place Pashua.app in /Applications, ~/Applications, or - in a pinch - in the same folder as the invoking script.
      • Pashua.app is the application that renders the dialogs (while a dialog is up, the menu bar shows Pashua).
    • Copy the bindings code (2 short handlers, showDialog and getPashuaPath) from Examples/AppleScript/Pashua.scpt into your script.
      • Note: Due to a bug as of this writing, you must apply a small correction to handler getPashuaPath: replace line return (path to applications folder from system domain as text) & "Pashua.app" with return (path to applications folder from system domain as text) & "Pashua.app:".
      • Alternatively, get the latest bindings code directly from the GitHub repo: https://github.com/BlueM/Pashua-Binding-AppleScript