Flylib.com

Books Software

 
 
 

Section 4.2. Getting Text Back from Dialog Boxes


4.2. Getting Text Back from Dialog Boxes

So 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.

Figure 4-1. The default answer option is all you need to add a text field to a dialog box. Whatever you put in your script after default answer will be what appears in this dialog box at first (top), although you're free to delete the text and enter anything you'd like instead (bottom).


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


The symbol, shown in the previous example, simply means "this command continues on the next line." This line-continuation symbol has no bearing on how AppleScript interprets your script, so you could theoretically retype the previous script as follows , and it would work exactly the same way:

set ¬
userResponse ¬
to the text returned ¬
of (display dialog ¬
"Enter your name:" ¬
default answer "Sylvester")
display dialog userResponse

In this book, scripts employ the line-continuation symbol because certain commands are simply too long to fit within the pages' margins. When entering these commands yourself, you can either type the symbol as written (by pressing Option-Return) or omit the symbol altogether and instead just type broken-up commands on a single line.

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.


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 cooler , however, if the script could give you a personalized greetingsomething like Figure 4-2.

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 tells AppleScript to "put together the strings on my left and right." Here's what the improved script would look like:

set userResponse to the text returned of (display dialog "Enter your name:" ¬
    default answer "Sylvester")

set theGreeting to "Hey, " & userResponse & "!

"

display dialog theGreeting


Up to Speed
Variable Usage

So far, you've used variables to store values for later lines in your scripts. Variables have restrictions and quirks all their own, however, and it's important to understand them so you can effectively write your own scripts.

For one, variable names can't be the same as preexisting AppleScript keywords. That means you can't write:

set text to "WiFi"

because text is an AppleScript keyword.

On the other hand, you're free to write:

set theText to "WiFi"

because the word theText has no significance in AppleScript. In fact, no two-words-squashed-together-as-one have any meaning in AppleScript, and that's why you'll often see variable names like myNumber , lastItem , and theAnswer .

You can include letters , numbers , and underscores (_ ) in your variable names, as long as the first character of the name isn't a number. Keep in mind, though, that variable names can't be more than 251 characters longanything longer, and you'll see the dialog box shown here.

Once you've set a variable, you just insert its name in your script to access the corresponding value. That's why you can write display dialog userResponse , for example, if you've set the userResponse variable earlier in your script.

Finally, you can set the same variable more than once in a script. If you wanted to, you could write something like this:

set myNumber to 4
set myNumber to (myNumber + 8)
set myNumber to (myNumber / 2)

In that example, the value of myNumber after the first line would be 4, after the second line it would be 12 (4+8), and after the third line it would be 6 ([4+8]/2).


Whenever you concatenate more than two strings (as in this example), you have to use ampersands between each item.

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.

Figure 4-2. Top : As in the previous scripts, you enter your name in the first dialog box. Bottom : However, when the second dialog box appears, you get a personalized welcome message, courtesy of AppleScript's text-handling capabilities.


{% if main.adsdop %}{% include 'adsenceinline.tpl' %}{% endif %}

There are other uses for concatenation, too:

  • Pulling the results of multiple commands into a single string . For example, if you wrote a script to administer computerized history tests, you could make your script concatenate all your responses together to create a summary of your answers.

  • Inserting punctuation into strings . You could use concatenation to surround a famous quote with quotation marks, for example.

  • Mixing numbers into strings . Just as you can concatenate two strings together, you can concatenate a string to a number . You might find that useful if you're writing a script to manage kitchen utensils, since you can combine the word "fork" with the number of clean forks left, for example.

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.