How can you prompt text in an alert?

Apple managed to prompt text in an alert when they ask for an administrator's password. How can I do this in Applescript?



This was the closest thing I can find, but with buttons.

set theAlertText to "An error has occurred."
set theAlertMessage to "The amount of available free space is dangerously low. Would you like to continue?"
display alert theAlertText message theAlertMessage as critical buttons {"Cancel", "Unlock"} default button "Continue" cancel button "Cancel"

Please notify me if you figure out, thanks!

Replies

Here is how you create an alert with a TextField.

Declare in the VC


     var alert : UIAlertController?


I have embedded in an IBAction to call the alert from a UIButton.


    @IBAction func testAlert(_ sender: Any) {
   
        //1. Create the alert controller.
        alert = UIAlertController(title: "Search City", message: "City name ", preferredStyle: .alert)
      
        //2. Add the text field.
        alert?.addTextField(configurationHandler: { (textField) -> Void in
            textField.placeholder = ""
            textField.keyboardType = UIKeyboardType.emailAddress // For instance
          // alertTextFieldDidChange activate OK button
            textField.addTarget(self, action: #selector(self.alertTextFieldDidChange(_:)), for: UIControl.Event.editingChanged)
        })
        
        //3. Grab the value from the text field.
        let yesAction = UIAlertAction(title: "Search", style: .default, handler: { (action) -> Void in
            let textField = self.alert?.textFields![0] 
            if textField?.text! == "" {
                print("empty")   // Just for testing, should be removed
            } else {
                print("not empty", textField?.text! ?? "")      // Just for testing, should be removed
            }
        })
        yesAction.isEnabled = false.
        alert?.addAction(yesAction)     // This will be actions[0]
      
        // 4. Present the alert.
        self.present(alert!, animated: true, completion: nil)
      
    }

The handler where we activate OK as soon as a textField not empty


    @objc func alertTextFieldDidChange(_ sender: UITextField) {

     alert?.actions[0].isEnabled = sender.text!.count > 0
    }

For information, from doc:


UIAlertController

var actions: [UIAlertAction]

The actions that the user can take in response to the alert or action sheet.



Declaration

var actions: [UIAlertAction] { get }

Discussion

The actions are in the order in which you added them to the alert controller. This order also corresponds to the order in which they are displayed in the alert or action sheet. The second action in the array is displayed below the first, the third is displayed below the second, and so on.


Regular AppleScript is limited to what you can do with display dialog, but AppleScriptObjC can be used to access the Cocoa APIs. For something like the password input, you would be looking at using an NSAlert with a couple of NSTextFIelds in an accessory view.