AppleScript_ The Definitive Guide - Matt Neuburg [133]
global r
set r to {name:"Matt", age:51}
on getWhat(what)
set s to "on run {r}
get " & what & " of r
end"
run script s with parameters {r}
end getWhat
getWhat("name")
It's a pity that such trickery is needed, and I don't really recommend this approach. Again, the List & Record Tools scripting addition provides a better alternative. See also "List Coercions" in the next chapter.
Chapter 14. Coercions
A coercion is a conversion of a value of one datatype to a value of another datatype. In AppleScript, not just any old value can be turned into a value of just any old datatype. To put it more strictly: for some pairs of datatype, call them Datatype A and Datatype B, it is the case that at least some values of Datatype A can be coerced to a value of Datatype B. For example, the string "30" can be coerced to a number; when that happens, you get the number 30. There are other strings that can be coerced to other numbers, and there are strings that can't be coerced to any number at all. This implies that there is some sort of equivalence or formula that determines the new value given the old value. This chapter presents these equivalences for every pair of datatypes, describing what coercions are possible and the rules by which they are performed.
The discussion is confined almost entirely to AppleScript's native datatypes (listed in Chapter 13). Coercions between these datatypes are defined by the language. Coercions between nonnative datatypes, or between a native datatype and a nonnative datatype, must be implemented by the application that defines the nonnative type. Applications do this, if they do it at all, on an individual basis, so no documentation is possible here. Additional coercions of native (and other) types can be implemented by scripting additions (Chapter 21), but these are not discussed here either.
Implicit Coercion
Implicit coercion is performed automatically when you supply a value where a value of another datatype is expected. This happens only 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.
Otherwise, AppleScript has no implicit coercion .
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 a parameter is passed to a user handler, because such handlers are not protected by any mechanism such as prototypes or datatype declarations in their definition from receiving parameters with undesirable datatypes. If your handler has reason to be choosy about what sorts of values it's willing to accept, then it needs to test those values and respond accordingly. For example, it might coerce explicitly, or it might throw an error, like this:
on sendMeAString(s)
if {class of s} is not in {string, Unicode text} then
error "Can't make some data into the expected type."
end if
-- remaining code goes here, secure in the knowledge that s is a string...
end sendMeAString
Coercion might take place when passing a parameter to a command, but then the coercion is probably not being performed by AppleScript, and whether it is implicit becomes a moot point. For example:
tell application "Finder"
set sidebar width of window 1 to "123"
end tell
You shouldn't have done that, but it works anyway. The sidebar width property is distinctly said, in the Finder's dictionary, to be an integer. You provided a string, but the Finder, instead of complaining, coerced it to an integer. This has nothing whatsoever to do with AppleScript itself! AppleScript did not look in the Finder's dictionary, see that the Finder expects an integer, and implicitly coerce the string. It sent the string