An AppleScript dialog textfield example

AppleScript dialog FAQ: How can I display an AppleScript dialog with a textfield (text field)?

A frequent AppleScript question is "How do I prompt a user to enter some text?" Here's how you display an AppleScript dialog to prompt a user to enter a simple piece of information, in this case their name:

display dialog "What is your name?" default answer ""

Running this AppleScript dialog code results in the following dialog:

An AppleScript dialog that prompts a user to enter their name.

Getting user input in an AppleScript textfield dialog

Of course prompting a user to enter some information isn't very useful unless you can get the information they entered. To do that you need to expand your AppleScript program a little, like this:

set theName to the text returned of 
  (display dialog "What is your name?" default answer "")

This displays the same dialog, but also gets the response from the user, and stores it in the variable named "theName".

If you run this program from the ScriptEditor, enter a name in the dialog, and press the OK button, the text you typed in will be shown in the Result tab in the ScriptEditor. If that's not enough for you, and you want to see this text in another dialog, you can change your program to look like this:

set theName to the text returned of 
  (display dialog "What is your name?" default answer "")
display dialog theName

This time, if you enter a name in the first dialog and then click OK, the text you entered will be displayed in a second dialog. It's also worth noting that if you click the Cancel button (or hit the [Esc] key) the second dialog will not be displayed, as the program stops at that point.