Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [143]

By Root 1647 0
For example:

tell application "Microsoft Entourage"

every contact contains contact 1

-- error: Can't make «class cAbE» id 1 of application "Microsoft Entourage"

into type vector

end tell

You should have said {contact 1}, or contact 1 as list (Entourage implements coercion to a list, unlike the Finder example in "Explicit Coercion" in Chapter 14). For the mysterious "vector" type mentioned in the error message, see "List" in Chapter 13.

Name

contains, does not contain, is in, is not in

Synopsis

containment

containment

Syntax

string1 contains string2

string2 is in string1

list1 contains list2

list2 is in list1

record1 contains record2

record2 is inrecord1

Description

Tests whether the first operand contains the second. The is in synonyms reverse the operand order of the contains synonyms—that is, with is in, the second operand comes physically before the first operand (as deliberately shown in the syntax listings), but it is still the second operand. This is relevant in the rules for implicit coercions.

Name

begins with

Synopsis

initial containment

initial containment

Syntax

string1 begins with string2

list1 begins withlist2

Description

Same as contains with the additional requirement that the second operand come first in the first operand. Records are not ordered, so they aren't eligible operands. Synonym is starts with.

Name

ends with

Synopsis

final containment

final containment

Syntax

string1 ends with string2

list1 ends withlist2

Description

Same as contains with the additional requirement that the second operand come last in the first operand. Records are not ordered, so they aren't eligible operands.

Concatenation Operator


Concatenation is the joining of two things in sequence. It may be performed on a pair of strings (resulting in a string), a pair of lists (resulting in a list), or a pair of records (resulting in a record). Implicit coercions are performed in exactly the same way as for the containment operators (see the previous section). So, for example:

"three" & 20 -- "three20"

3 & "twenty" -- {3, "twenty"}

That example shows the difference the order of operands can make; the reason is perfectly obvious if you know the implicit coercion rules, and baffling otherwise.

In earlier versions of AppleScript, concatenation of Unicode text and a string was troublesome, because the class of the result depended the class of the first operand. Now (starting in Tiger), if either operand is Unicode text, the result is Unicode text. This behavior makes string concatenation effectively transparent.

To turn string concatenation into list concatenation, it suffices to coerce the first operand to a list ; this can be done simply by expressing it in list delimiters:

{"Mannie"} & "Moe" & "Jack" -- {"Mannie", "Moe", "Jack"}

Without the list delimiters, we'd end up with "MannieMoeJack".

Recall (from Chapter 14) that coercion of a list to a string is another way to concatenate. Thus concatenation of a string and a list concatenates the string with all the elements of the list, each coerced to a string and joined by the text item delimiters:

set text item delimiters to ""

"butter" & {"field", 8} -- "butterfield8"

Recall what was said in the previous section about both operands having to be of the same type, and what this implies for lists. Concatenation is a way to append one or more items to a list:

{1, 2, 3} & {4, 5, 6} -- {1, 2, 3, 4, 5, 6}

The result is not {1, 2, 3, {4, 5, 6}}; if that's what you wanted, you can use an extra level of list delimiters:

{1, 2, 3} & {{4, 5, 6}}

Concatenation of a list to the empty list yields exactly the same list, not a copy. This is probably intended as an optimization, but you are more likely to experience it as a bug. For example:

set L1 to {}

set L2 to {"mannie"}

set L1 to L1 & L2

set item 1 of L1 to "moe"

L2 -- {"moe"}

Evidently L1 has been set (by reference) to L2; L1 and L2 are now two names for the same thing. This would not have happened if the first operand in the concatenation

Return Main Page Previous Page Next Page

®Online Book Reader