Section 4.3. Linking Strings Together


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


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.



AppleScript. The Missing Manual
AppleScript: The Missing Manual
ISBN: 0596008503
EAN: 2147483647
Year: 2003
Pages: 150

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net