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 option default answer which 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
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
|
4.3. Linking Strings Together
In the previous script, the second command simply spits back whatever text is in the dialog box's text field when you press OK. It would be much
To achieve this feat of textual impressiveness, you have to use a feature known as
concatenation
linking multiple strings together into one. In AppleScript, the way you concatenate strings is with an ampersand (
&
), which
set userResponse to the text returned of (display dialog "Enter your name:" ¬
default answer "Sylvester")
set theGreeting to "Hey, " & userResponse & "!
"
display dialog theGreeting
When the script runs, it combines the three strings"Hey, " whatever is entered as userResponse , and the exclamation marktogether. The result is that all three items appear together as one big string ( theGreeting ) in your final dialog box, as shown at the bottom of Figure 4-2.
{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %} There are other uses for concatenation, too:
As you become more and more familiar with AppleScript, you're sure to find many other ways to put concatenation to work for you. Just remember: if you're concatenating more than two items together, you must use a separate ampersand between every two items. |