Chapter 14. Coercions
A coercion is a conversion of a value of one datatype to a value of another datatype. This definition implies that there is some sort of equivalence or formula that determines the new value given the old value. In AppleScript, not just any old value can be turned into a value of just any old datatype. This chapter describes how coercions are performed and what coercions are possible.
Implicit Coercion
When you supply a value where a value of another datatype is expected, AppleScript may coerce silently if possible. This is called implicit coercion, and it takes place in connection with AppleScript’s operators. These operators have definite rules about what datatypes they expect, and what implicit coercions they will perform if other datatypes are provided. Details appear in Chapter 15.
No implicit coercion takes place when assigning a value to a variable, because variables have no declared datatype; the variable simply adopts the new value.
No implicit coercion takes place when passing a parameter, because handlers and commands do not provide prototypes specifying a particular datatype. This is not to say that a handler or command cannot itself perform a coercion if it receives one datatype and prefers another. It can. But then the coercion is not necessarily implicit, and it isn’t necessarily being performed by AppleScript.
For example, suppose you say this:
tell application "Finder"
set name of folder 1 to 6
-- error: "Can't make some data into the expected ...
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.