Concatenation Operator

Concatenation 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 Section 15.4, just previously.

So, for example:

"three" & 20 -- "three20"
3 & "twenty" -- {3, "twenty"}

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

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. So:

{"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 (Section 15.4) 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: ...

Get AppleScript: The Definitive Guide 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.