AppleScript_ The Definitive Guide - Matt Neuburg [122]
set s to "2/25"
set d to date s -- date "Friday, February 25, 2005 12:00:00 AM"
set d2 to date "10:30" of d -- date "Friday, February 25, 2005 10:30:00 AM"
set d3 to date "1/24" of d2 --date "Monday, January 24, 2005 10:30:00 AM"
Date properties
Setting a property of a date mutates the date in place. You can change a property in a calendrically impossible way, and AppleScript will compensate:
set s to "May 31"
set d to date s
set month of d to June
d -- date "Friday, July 1, 2005 12:00:00 AM"
Now that a date has hours , minutes , and seconds properties (new in Tiger), this technique is actually useful for date calculations:
set s to "8/10/2005 10:00 PM"
set d to date s
set hours of d to ((hours of d) + 100)
d -- date "Monday, August 15, 2005 2:00:00 AM"
When you use set (as opposed to copy) to set a variable to a value that is a date, you set the variable by reference. This means that the date is not copied; the variable's name becomes a new name for the date, in addition to any names for the date that may already exist. The same is true when a date is passed as a parameter to a handler. This special treatment is in common between lists, records, dates, and script objects, the four datatypes that can be mutated in place. (See "Set by Reference" in Chapter 7 and "Pass by Reference" in Chapter 9.)
For example:
set s to "May 31"
set d to date s
set d2 to d
set month
of d2 to June
d --date "Friday, July 1, 2005 12:00:00 AM"
Date Properties
The following are the properties of a date value:
year
The year number. A positive integer.
month
The month. A constant (not a string!): January, February, and so on. However, you can set a date's month using an integer (this is new in Tiger).
day
The day of the month. A positive integer.
hours
Whole hours since the start of the day (at midnight). A positive integer.
minutes
Whole minutes since the start of the hour. A positive integer.
seconds
Seconds since the start of the minute. A positive integer (hours, minutes, and seconds are new in Tiger).
time
Seconds since the start of the day (at midnight). A positive integer.
weekday
A constant (not a string!): Monday, Tuesday, and so on. In practice this property is read-only; setting it is not an error, but it has no effect.
date string
short date string
time string
A string consisting of just the date or time part of the date-time. In practice these properties are read-only; setting them results in a stack overflow (I'd have to call that a bug). They are formatted in accordance with your International preferences.
The time string and date string are suitable for combining with an existing date to form a new date, using the syntax described earlier. For example:
set s to "5/25/2005"
set d1 to date s
set t to "4PM"
set d2 to date t
set d3 to date (time string of d2) of d1 -- date "Wednesday, May 25, 2005 4:00:00 PM"
String
A string is the basic text datatype. It has the MacRoman encoding; see "Unicode Text," later in this chapter, for more about the implications of this. Strings are your primary medium for communicating text information to a scriptable application. AppleScript provides some basic string operators (discussed in Chapter 15).
A literal string is delimited by quotation marks:
set s to "howdy"
class of s -- string
The empty string is symbolized by "".
In typing a string literal, you may enter certain characters in "escaped " form; they are listed in Table 13-1. These are the only "escaped" characters.
Table 13-1. "Escaped" string literals
What to type
ASCII equivalent
Result
\"
ASCII character 34
Quotation marks
\t
ASCII character 9
Tab
\r
ASCII character 13
Return
\n
ASCII character 10
Newline
\\
ASCII character 92
Backslash
Other untypeable characters may be generated using the ASCII character scripting addition command and incorporated into a string by concatenation (see chapters 15 and 21). There are also a few global properties expressing character