Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [144]

By Root 1628 0
had originally been anything other than an empty list. One wants to say, "So just don't do that," but the problem is that if (as in this example) the first concatenation operand is a variable, you might not know that it is an empty list, and it seems onerous to suggest that you should test it every time. A workaround is to use copy instead of set in the third line:

copy L1 & L2 to L1

The operation set end of is more efficient than the concatenation operator because no extra copies have to be made internally. Strings are not mutable in place, but lists are. Thus it is common practice to perform an extended series of string operations by operating on a list instead. For example, instead of this:

set s to "anti"

set s to s & "dis"

set s to s & "establishment"

set s to s & "arianism"

it is more efficient to say this:

set text item delimiters to ""

set L to {}

set end of L to "anti"

set end of L to "dis"

set end of L to "establishment"

set end of L to "arianism"

set s to L as string

Concatenating records yields a record consisting of all the items of the first record along with just those items of the second record whose name isn't the name of any item in the first record (see Chapter 13):

set r to {who:"Jaime", town:"Ojai"} & {who:"Matt", friend:"Steve"}

r -- {who:"Jaime", town:"Ojai", friend:"Steve"}

Concatenating a record with an empty list (in either order) yields the same record. For a use of this, see "Parameters" in Chapter 9. If the empty list is the first operand, the result is the very same record, just as we saw a moment ago for lists:

set R1 to {}

set R2 to {name:"Matt"}

set R1 to R1 & R2

set name of R1 to "Neuburg"

R2 -- {name:"Neuburg"}

Scripting additions can provide further interesting variations on the notion of concatenation. For example, the Satimage scripting addition's special concat command concatenates lists from items with the same name in different records:

special concat {who:{"Matt"}} with {who:{"Neuburg"}} -- {who:{"Matt", "Neuburg"}}

Name

&

Synopsis

concatenation

concatenation

Syntax

string1 & string2

list1 & list2

record1 &record2

Description

Concatenates the operands. The result is a string, list, or record respectively.

Parentheses


Parentheses may be used to determine the order of operations at runtime:

3 + 4 * 2 -- 11

(3 + 4) * 2 -- 14

Parentheses can also help determine the order of interpretation of vocabulary at compile time. Thus they can make the difference between successful compilation and failed compilation. For example, this compiles fine, because all the expressions are legal:

set r to random number

round r rounding up

Now try to save a line by combining them, and you get this ungrammatical and mysterious error:

round random number rounding up

-- compile-time error: A application constant [sic] or consideration can't

go after this identifier

The problem is that random number is a command that can optionally take various labeled parameters, and rounding up isn't one of them. Instead of rethinking its interpretation ("So, maybe random number isn't taking any parameters here!"), AppleScript just gives up. You have to help it out, by using parentheses:

round (random number) rounding up

Sometimes AppleScript will insert parentheses for you, on compilation. For example, I didn't put any parentheses when I typed this code:

tell application "System Events"

copy name of every process where it is frontmost to theProc

end tell

But AppleScript did, when it compiled:

tell application "System Events"

copy (name of every process where it is frontmost) to theProc

end tell

The reason seems to be to delimit a phrase implying a get command. But if you actually use get explicitly here without parentheses, AppleScript refuses to compile at all:

tell application "System Events"

copy get name of every process where it is frontmost to theProc

-- compile-time error: Expected "into", "to", etc. but found "get"

end tell

The problem seems to be that AppleScript doesn't like the phrase copy get, which is two

Return Main Page Previous Page Next Page

®Online Book Reader