String, Number and Date Coercions
A class or enumerator may be coerced to a string; for example:
string as string -- "string"
A number may be coerced to a string. A string may be coerced to a
number, provided it looks like a literal number; whitespace will be
ignored, but nothing else will be. So for example
"1a
" can’t be coerced to a
number. But the empty string, or a string consisting solely of
whitespace, will be coerced to 0
.
An
integer may be coerced to a real. A
real may be coerced to an integer; it is rounded to the nearest
integer. This is a new feature; in earlier versions of AppleScript, a
real could be coerced to an integer only if it
was an integer. For example,
1.5
couldn’t be coerced to an
integer. The round
scripting addition command can help
here (see Section 20.5.6), and can be used to dictate
the desired rounding behavior.
A date may be coerced to a string; this is simply the string that appears in the literal date specifier after compilation. A string may be used to form a date specifier, but it cannot be coerced to a date. A month may be coerced to a string (because it is a class). A month may also be coerced to an integer; this is a new feature.
A string,
Unicode text, and
styled text may be coerced
to one another. When coercing to a string, you can say as text
instead of as string
. This is
confusing, since the class of the result is still
string
, and text
is actually
the name of a completely different class (string
is 'TEXT
', text
is
'ctxt
').
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.