Section 5.1. Lines


5.1. Lines

AppleScript is a line-based language. There is no visible command terminator, such as a semicolon; a command ends with and is separated from the next command by a line break. For example:

 set x to 1 copy x + 1 to y display dialog y

You can't have one complete command and then another complete command in the same line, but you can have one command nested inside another command, because most commands in AppleScript have a result (see "Result," later in this chapter), and most commands have at least one parameter, so the result from one command can often be used as the parameter to another command, in the same line. So, for example, in the second line of this code:

 set x to 1 copy (display dialog x) to y

The only commands that can't be nested inside other commands in this way are a few special verbs implemented deep inside AppleScript, such as set and copy.

It is legal for a line to be completely blank. Extra whitespace (spaces, tab characters) is legal and will be ignored.

5.1.1. Line-Break Characters

In a decompiled script, the breaks between lines are all Macintosh return characters (\r, ASCII character 13). This is somewhat ironic, because in a present-day script editor application such as Script Editor or Script Debugger, when you press the Return key what you're entering is a Unix newline (\n, ASCII character 10). Thus the line-termination character goes into the compiler as a Unix line break and comes back out on decompilation as a Macintosh line break.

Indeed, in a script text file (or other text to be treated as AppleScript code) it doesn't matter whether the line-break character is the Macintosh line break, the Unix line break, or even the Windows line break (\r\n). If the code is valid, AppleScript will be able to compile it regardless (and all the breaks between lines of code will end up as Macintosh line breaks internally).

5.1.2. Line-Break Characters in Strings

It is legal to type a line break in a literal string (that is, between matched pairs of double quotes). This represents a line-break character within the string. For example:

 set pep to "Manny Moe Jack"

The line-break character being inserted here is the line-break character you get when you press the Return key. What character that is depends upon the editing environment and the era. In the old days it was the Macintosh line break (\r); nowadays, as I mentioned already, it is the Unix line break (\n).

AppleScript also permits the representation of line-break characters through the return global property (see Chapter 16) and the "escaped" characters \r and \n (see "String" in Chapter 13). Thus your code can construct a string value containing a line-break character by using these representations. When you display such a string value, it will be shown (in most contexts) with a visible line break. For example:

 set pep to "Manny" & return & "Moe" return pep

The result (the value of pep) is displayed like this:

 "Manny Moe"

So too for a decompiled string literal containing an "escaped" line-break character. The change happens right within your script. If you type this:

 set pep to "Manny\rMoe\nJack"

then when you compile (and decompile) you'll see this:

 set pep to "Manny Moe Jack"

This behavior, by general agreement, is annoying, not least because (as the example illustrates) you have no way of knowing whether the line break in the decompiled representation is a Macintosh or a Unix line break. (Script Debugger solves this problem; it lets you view invisible characters in your script, and permits a literal string value to be displayed with "escaped" characters.)

5.1.3. Continuation Character

Long lines of code are inconvenient if your script editor application does not wrap them. In such an environment, if your code contains long lines and you want to be able to read them without scrolling horizontally, you need a way to break long lines. Because a line is a meaningful syntactic unit, you cannot do this merely by inserting a return character. Rather, you must also enter a continuation character.

The problem the continuation character solves is no longer much of a problem, though, because current script editor applications do wrap long lines.


The continuation character appears as the "logical not" sign; it is MacRoman codepoint 194, Unicode (and WinLatin1 and ISOLatin1) codepoint 172. This character is usually typed on Macintosh as Option-L; but as a convenience, in a script editor application, typing Option-Return enters both the logical-not character and a return character, and is the usual way of continuing a line. For example:

 set a to 

1

It is a compile-time error for anything to follow the continuation character on the same line other than whitespace.

It is a compile-time error for the line following the continuation character to be blank, unless what precedes the continuation character is a complete command, as in this very silly example:

 set a to 1 

  set b to 2

A continuation character inside a literal string is interpreted as a literal logical-not character. To break a long literal string into multiple code lines for legibility without introducing unwanted return characters into the string, you must concatenate multiple literal strings:

 set s to "one very long line " & 

"deserves another"

Under some circumstances, AppleScript will move or remove your continuation characters at compile time. There's nothing you can do about this; it's an effect of the decompilation process. See "Decompiling" in Chapter 3.

In this book, long lines are manually broken for legibility. Continuation characters are inserted to indicate such breaks, without regard for whether AppleScript would move or remove these continuation characters in a compiled version of the script.





AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

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