Prompting for Text
Use the display dialog command’s optional default answer parameter to collect text, such as a username or email address, as your script runs. As demonstrated by Figure 23-1, Listing 23-1, and Listing 23-2, the inclusion of the default answer parameter automatically adds a text entry field to the resulting dialog. Any string you provide for the parameter appears in the text field when the dialog displays. Providing an empty string ("") produces an empty text field. When the dialog dismisses, any text from the field is returned in a text returned property of the display dialog command’s result.
APPLESCRIPT
set theResponse to display dialog "What's your name?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"--> {button returned:"Continue", text returned:"Jen"}display dialog "Hello, " & (text returned of theResponse) & "."
JAVASCRIPT
var app = Application.currentApplication()app.includeStandardAdditions = truevar response = app.displayDialog("What's your name?", {defaultAnswer: "",withIcon: "note",buttons: ["Cancel", "Continue"],defaultButton: "Continue"})// Result: {"buttonReturned":"Continue", "textReturned":"Jen"}app.displayDialog("Hello, " + (response.textReturned) + ".")
Prompting for Hidden Text
Protect potentially sensitive information from prying eyes by using the display dialog command’s default answer parameter in conjunction with the hidden answer parameter to show bullets instead of plain text in the dialog’s text field. See Figure 23-2, Listing 23-3, and Listing 23-4.
APPLESCRIPT
display dialog "Please enter a passphrase to use this script." default answer "" with icon stop buttons {"Cancel", "Continue"} default button "Continue" with hidden answer--> Result: {button returned:"Continue", text returned:"MySecretPassphrase"}
JAVASCRIPT
var app = Application.currentApplication()app.includeStandardAdditions = trueapp.displayDialog("Please enter a passphrase to use this script.", {defaultAnswer: "",withIcon: "stop",buttons: ["Cancel", "Continue"],defaultButton: "Continue",hiddenAnswer: true})// Result: {"buttonReturned":"Continue", "textReturned":"MySecretPassphrase"}
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13
