AppleScript_ The Definitive Guide - Matt Neuburg [139]
-- {"pep", 3, {"Mannie", "Moe", "Jack"}}
Unit Conversions
AppleScript provides a number of classes whose sole purpose is to allow you to perform measurement unit conversions. They are implemented as classes so that you can use the as operator to perform the conversion; that is, the conversion is really a coercion.
Because of this implementation, the way you have to speak in order to perform a conversion ends up looking fairly silly. You can't say 3 feet; you have to coerce 3 (a number) to the feet class, by saying 3 as feet. Now you coerce to the desired class; suppose this is yards. But now you have a value of the yards class. You can't do anything with it, so you have to coerce it to a number (or a string). So, for example:
on feetToYards(ft)
return ft as feet as yards as number
end feetToYards
feetToYards(3) -- 1.0
The implemented units are themselves a mixed lot. Many important units, such as acres and hectares, aren't implemented at all. Accuracy of some of the conversions has also been called into question, but this is said to be fixed in Tiger. Table 14-1 provides a list.
Table 14-1. Conversion unit classes
meters
inches
feet
yards
miles
kilometers
centimeters
square meters
square feet
square yards
square miles
square kilometers
liters
gallons
quarts
cubic meters
cubic centimeters
cubic feet
cubic inches
cubic yards
kilograms
grams
ounces
pounds
degrees Celsius
degrees Fahrenheit
degrees Kelvin
A better list of conversion units is built into Mac OS X by way of the Unix tool units. Here's a way to use it:
on convert(val, unit1, unit2)
set text item delimiters to " "
set conv to do shell script ({"units", unit1, unit2} as string)
return val * (word 1 of paragraph 1 of conv as real)
end convert
convert(4, "feet", "meters") -- 1.2192
Chapter 15. Operators
An operator is a token that transforms a value or a pair of values to produce a new value. These transformations are operations, and the values operated upon are the operands. An operator with two operands is binary; an operator with one operand is unary. This chapter lists the AppleScript operators and explains what they do, with special attention to implicit coercions performed by the operators. It also talks about parentheses, because they help determine the effects of the operators. Finally, there's a section on the differences between what happens when AppleScript performs an operation and when a scriptable application performs it.
(For the coercion operator, as, see Chapter 14; for the object containment operator, of, see Chapter 11.)
Implicit Coercion
In Chapter 14, I explained coercion and described the coercions that are possible between built-in datatypes in the AppleScript language. Binary operators can (and will) perform coercion without your specifically asking for it. This is called implicit coercion , and is one of the most confusing aspects of AppleScript—and a frequent source of mistakes in scripts. If you are not prepared for what implicit coercions an operator will perform, you will be surprised when the result of an operation is not what you expected. That's why this chapter spends so much time on the implicit coercions performed by the various operators.
What coercions AppleScript will perform implicitly is not the same as what coercions you can get it to perform explicitly (with the as operator). AppleScript's error messages in this regard can add to the confusion. For example:
1 and 1 -- compile-time error: Can't make 1 into type boolean
That error message, on its face, is lying. AppleScript can make 1 into a boolean. What AppleScript really means by the error message here is: "In order for me to perform this operation, I would need to coerce the first 1 (before the and) to a boolean, implicitly; and I refuse to do that." Even weirder is what happens when you proceed to coerce the first 1 to a boolean explicitly:
1 as boolean and 1 -- true
It works—which means that even though AppleScript refused to coerce the first 1 to