Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [121]

By Root 1501 0
it remains a real:

1e2 -- rewritten: 100.0

2.1e26 -- rewritten: 2.1E+26

A literal number consisting of too many digits (I have not been able to determine exactly how many is too many) may be rounded, or may be rewritten using scientific notation, at compile time. Alternatively, it may generate an incomprehensible error:

0.123456789012345 -- Syntax error. some object

[sic]

You can't include a comma as a thousands separator in a literal number.

The class number is purely for purposes of coercion. See "Number, String, and Date Coercions" in Chapter 14.

A dictionary may occasionally mention a class small integer , which is two bytes (ranging from -32768 to 32767). You can create one by coercion, but there should be little need to do so (though an example appears later in this chapter). Small integers are typically used transparently; they evidently become integers before you get a look at them:

set x to 4 as small integer

class of x -- integer

class of (ASCII number "a") --integer, even though the dictionary says "small integer"

There is also a class double integer , which is eight bytes. This is sometimes used when communicating with the system, and seems to be simply a real within the integer range. Again, there should be little need to create one; a double integer in your code is reported as a real. There are other rarely encountered numeric classes, such as fixed, extended real , and so forth, which are reported as (or transparently coerced to) integer or real.

Date


A date is a date-time. A literal date is an object string specifier . In constructing a date, you may use any string value that can be interpreted as a date, a time, or a date-time; AppleScript (or more probably the system) is quite liberal in what it will accept, provided that the string makes sense in terms of the date and time format settings in your International System Preferences . AppleScript supplies missing values such as today's date (if you give only a time) or this year (if you don't give a year) or midnight (if you give only a date). To form a date object for the current date-time, use the current date scripting addition command (see Chapter 21).

AppleScript presents a literal date specifier in long date-time format in accordance with your International preferences. It does this even within your script, on decompilation, if you use a literal string in a date specifier:

date "5/25/2005" -- rewritten: date "Wednesday, May 25, 2005 12:00:00 AM"

If the expression "5/25/2005" isn't a date according to your International preferences, this code won't compile. For example, if you have U.K. settings, you'd need to type date "25/5/2005". Scripts that form dates dynamically by coercing from a string, like most of the examples in this section, are subject to the same caveat (and are thus not very portable).

AppleScript knows nothing of time zones , and assumes the Gregorian calendar even for dates before its invention. An attempt to form a date specifier earlier than the start of 1000 AD will fail:

set s to "December 25, 800"

date s -- date "Monday, December 25, 2800 12:00:00 AM"

Confusingly, however, you can obtain such a date by calculation:

set s to "December 25, 1000"

set d to date s

set year of d to 800

d -- date "Monday, December 25, 0800 12:00:00 AM"

Internally, a date is stored as a number of seconds; precision higher than a second is thrown away during calculation. There are three ways to do calculations with dates:

Date arithmetic

You can derive one date from another by adding or subtracting a number representing seconds. (Chapter 16 lists some global properties that can help you calculate the desired number of seconds.) One date may also be subtracted from another to obtain the number of seconds between them. For example:

set s to "8/10/2005 4:45 PM"

(date s) + 56845 -- date "Thursday, August 11, 2005 8:32:25 AM"

Date specifier property

By a curious syntax, a new time part or date part may be combined with an existing date by treating a date specifer as a property of another date. The result

Return Main Page Previous Page Next Page

®Online Book Reader