I am using applescript to take dates from excel spreadsheets, and depending on the category, I need it to either add the date as is in ical as a todo, and in other cases i need to add 1 week, or 2 weeks, to the time frame.
I got 1 week to work by adding:
if item i of summaryList is "ZZZ" then set theDate to theDate + weeks
But how do I add 2 weeks?
And what if i want to add a specific amount of days? i.e. 3 days
I believe (but don’t know for sure) that the reason AppleScript’s time constants have plural names is to allow vaguely English-looking expressions when numbers are multiplied by them:
(current date) + 10 * years -- (Error: "The variable years is not defined.")
(current date) + 10 * year -- (Error: "Can't make year of <script> into type number.")
(current date) + 10 * months -- (Error: "Can't make every month of <script> into type number.")
(current date) + 10 * seconds -- (Error: "Can't make seconds into type number.")
What’s up with these? Is there similarly a syntactically elegant way of adding years, months and seconds, without resorting to convoluted calculations? (Which I know how to do - just wondering.)
Ah! Of course! AppleScript bases everything on seconds. This is the crux of why AppleScript cannot directly so work with years & months; one therefore must resort to the kind of what I called “convoluted calculations” as Stefan described. Thanks, Stefan!
That this type of calculation works depends on the the fortunate property of AppleScript to do what might be called “modulo overflow” when the values exceed the limits of, e.g., months in a year: when you add enough months, it’s smart enough to overflow into incrementing the year, too.
But I’ve noticed that when you add too large a number past a certain point (for which I haven’t discerned rhyme or reason yet), it doesn’t overflow correctly:
For example, with:
current date = date “Wednesday, November 4, 2009 11:35:18 AM”:
set newDate to (current date)
set month of newDate to (month of newDate) + 4
newDate
newDate = date “Wednesday, November 4, 2009 11:37:41 AM”
…but incrementing one more month:
set newDate to (current date)
set month of newDate to (month of newDate) + 5
newDate
…yields the unwanted newDate = date “Friday, September 27, 2030 11:41:16 AM”
Yes, thanks - very useful article. Actually I had found it earlier, and it acknowledges the excessive overflow errors I mentioned, and thankfully comes up with some algorithms to workaround them. I guess I was just wondering whether anyone knows why AppleScript fails so abruptly after directly adding only so few months out? Is it a bug (or a feature)?
Or a “fug”?
Definition - FUG: noun, a F_eature that would be called a b_UG, had not the usability-clueless programmers deliberately included it.
Common usage - “To Microsoft”: “To FUG-up!”
It’s not a bug - it’s an undocumented feature.
If it happens once, it’s a bug. If it happens twice, it’s a feature. If it happens more than twice, it’s a design philosophy.