4.2. Getting Text Back from Dialog BoxesSo far, all you've done with dialog boxes is display information. That won't help you much if you want to get feedback to use in your scripts, though. Luckily, the display dialog command supports an extra optiondefault answerwhich lets you type text into your dialog boxes as well. Try running this script: display dialog "Enter your name:" default answer "Sylvester" A dialog box appears on screen, with a text-entry field inside (Figure 4-1). Still, even though you can enter text in your dialog boxes with the default answer option, you can't capture the text you entered and reuse it elsewhere in the script. Or so you might think.
Luckily, using the power of variables, you can store the response to your dialog boxes for later use. Try running this script: set userResponse to the text returned of (display dialog "Enter your name:" ¬ default answer "Sylvester") display dialog userResponse
set ¬ userResponse ¬ to the text returned ¬ of (display dialog ¬ "Enter your name:" ¬ default answer "Sylvester") display dialog userResponse
In this script, AppleScript sets the userResponse variable to the text you enter in the dialog box. Then on the next line, that text is echoed back to you in a new dialog box. |