On Sun, May 23, 2010 at 6:12 PM, Gil Dawson
<email@hidden> wrote:
Is there a way to code a literal date into a script in such a way that the same text would compile on both machines?
Nope. If you compile it on either machine, it will run fine on the other, but if you want to share source code text, you can't use the date literal string syntax.
My usual solution is to do something like this:
set myDate to current date
tell myDate to set {its month, its day, its year} to {October, 31, 2015}
But as written, that's not a completely general solution. Order matters, because it sets each component individually and error-corrects the others. Consider this example:
set mydate to current date
set mydate to mydate + 7 * days
log (mydate)
(*date Sunday, May 30, 2010 6:30:16 PM*)
tell mydate to set {its month, its day, its year} to {February, 15, 2011}
mydate
--> date "Tuesday, March 15, 2011 6:30:16 PM"
As soon as it set "month" to February, the date would have been illegal (Feb 30), so it shifted to March 2nd. Then it set the day and year, yielding the above incorrect final result. This isn't a problem if you're setting a date in October or any other month with 31 days, but the general solution is to take an extra step to set the month to one of those even when it's not the ultimate goal:
set myDate to current date
tell myDate
set its month to January -- or any other month with 31 days
set {its day, its year} to {targetDay, targetYear}
set its month to targetMonth
end tell
Mark J. Reed <