Section 4.4. Multiline Strings


4.4. Multiline Strings

If you've spent any time working in a word processing program like TextEdit or Microsoft Word, you already know that text often spans multiple lines. Unlike typewriters from days of old, you don't have to hit the Return key when you reach the end of a line. Instead, the text just wraps around to the next line, and you continue typing until you reach the end of a paragraph.

There are, however, certain times when you might like to break text up onto multiple linessuch as typing your mailing address at the top of a letterand for that, the trusty Return key is right there by your side. But what if you want to use such multiline strings in AppleScript, so you can display a dialog box containing a friend's address, for example?

Thankfully, AppleScript supports multiple-line strings as well, but you'll need to enlist some help from escape sequences. Escape sequences are little chunks of slashes and letters that tell AppleScript, "Please insert a special symbol here."

To insert a newline symbol in a stringthus knocking text onto the next lineyou use the \n escape sequence. For example, if you ran the following script, you'd see the dialog box shown in Figure 4-3:

set multiLiner to "To whom it may concern,\nI want my money back.\nThanks." display dialog multiLiner

If you find the \n sequence too geeky, you can instead just type Return in the middle of a string. For example, you could rewrite the above script as follows:

set multiLiner to "To whom it may concern, I want my money back. Thanks" display dialog multiLiner

and it would work exactly the same way. Still, you'll often see older scripts use the \n sequence, so remember that it works the same way.

Figure 4-3. Using the escape sequence \n (which stands for "new line"), you can easily make strings that run multiple lines.


There are other escape sequences you can use in your strings, too (Table 4-1 has a list of the most common). Such escape sequences allow you to make AppleScript strings that match what you could create in a TextEdit or Microsoft Word document. That way, you can display dialog boxes with all sorts of punctuation insidenot just letters and numbers.

Table 4-1. AppleScript escape sequences

Sequence

What it represents

Example string

\n

A newline character.

"1: Shine shoes\n2: Eat lunch"

\r

A "carriage return"the way that certain older Mac programs indicate that text should continue onto the next line. Some programs use \n to break a line, some require \r, and some will accept either (you have to test the program to see which one it accepts).

"1: Shine shoes\r2: Eat lunch"

\t

An indentation character (the same as pressing Tab); this character will show up as a single space in a dialog box but will show up as a true tab in a program like TextEdit or Microsoft Word.

"Cost\t\Number sold\tProfit"

\"

A double quote within a string.

"She said, \"Hello\" to me."

\\

A single backslash character.

"The haiku said: AppleScript is good. \\ Concatenation is a \\ quite long word indeed."


You should type each string example from Table 4-1 on a single line.



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