Unit of Measurement Classes
AppleScript provides six categories of classes for representing area, length, temperature, cubic volume, liquid volume, and weight measurements. These data types are very handy for converting measurements within the categories:
square feet
square kilometers
square kilometres
square meters
square metres
square miles
square yards
centimeters
centimetres
feet
inches
kilometres
kilometers
metres
meters
miles
yards
degrees
Celsius
degrees
Fahrenheit
degrees
Kelvin
cubic
centimeters
cubic
centimetres
cubic feet
cubic inches
cubic metres
cubic meters
cubic yards
gallons
litres
liters
quarts
grams
kilograms
ounces
pounds
You can only coerce back and forth between measurement classes within the same category. The return values for these classes are the name of the data type followed by a space and its value. The return value of:
set theMeters to 6000 as meters
is “meters 6000.0” and its class is
meters
. The following code illustrates what is and
is not permitted in terms of using these classes. The end of the code
example shows the results from Script Editor’s Event
Log window:
set theMeters to 6000 as meters set theFeet to theMeters as feet log theFeet set theReal to (theFeet as inches) as real log theReal set cubYards to theReal as cubic yards log cubYards set cubYards to cubYards as yards Event log: (*feet 1.96850394E+4*) -- meters to feet returns as class 'feet' (*2.362204728E+5*)-- feet to inches to real returns as class 'real' (*cubic yards 2.362204728E+5*) -- real to cubic yards returns as class 'cubic yards' --> Can't make cubic yards 2.362204728E+5 into a yards. (* returns an error, because you cannot coerce a 'cubic yards' class into a 'yards' class. *)
You can convert meters
to
feet
(6000 meters is equivalent to about 19,685
feet), because both of these classes are in the
“length” category. The previous
example then converts feet
to
inches
(also permitted because they are both
length units) and stores that result as a real
value type (i.e., a floating-point number that represents the number
of inches in 6,000 meters). Then, since the number (stored in the
variable theReal
) is a real
value type rather than a length-unit type, it can be coerced to
cubic yards
. cubic yards
is in
the cubic-volume category. But cubic yards
cannot
be coerced to yards
, which is in the length
category, thus giving rise to the error in the prior example.
These groups of data types make it
very easy to create utility functions for calculating measurements.
The next example is a subroutine that converts meters to feet or feet
to meters (see Chapter 8). The second parameter to
the ConvertMe function is a
boolean
. If it’s
true
, then the conversion is from
meters
to feet
. If
false
, then the conversion is from
feet
to meters
. Another way to
accomplish this task is to check the class of a single parameter
(i.e., val
). If its class is
feet
, then convert it to
meters
, and vice versa. If its class is neither,
then write the subroutine to return an error code such as -1.
on ConvertMe(val, toFeet) if toFeet then -- 3.2808399 feet equals one meter set conResult to val * 3.2808399 set replyToUser to "Converting " & val & " meters to feet equals:¬ " & (round conResult) & " feet" -- use round scripting addition display dialog replyToUser (* display the conversion result using display-dialog scripting addition *) else -- one foot equals 0.304799999537 meters set conResult to val * 0.304799999537 set replyToUser to "Converting " & val & " feet to meters equals:¬ " & (round conResult) & " meters" -- use round scripting addition display dialog replyToUser (* display the conversion result using display-dialog scripting addition *) end if end ConvertMe