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

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.