How do I Employ CJK's AppleScript "makeASDate" Handler Without Error?
I am attempting to employ the makeASDate handler shared by user CJK (which I lifted from his answer at Apple Script: Can’t get date of "2018-12-12 10:00 AM") in order to set up some calendar events, one per day, from a Numbers worksheet containing Date, Reading, and Page Numbers.
Here is my code, thus far:
set CalendarDates to {}
set CalendarDateMonths to {}
set CalendarDateDatesOfMonths to {}
set Passages to {}
set PageNumbers to {}
set theYear to 2021
tell application "Numbers" to tell the front document to tell the active sheet to tell table 1
repeat with i from 2 to the count of cells of column "A" -- row 1 is a header
set theCalendarDate to formatted value of cell i of column "A"
set theCalendarDateMonth to the first word of theCalendarDate
set theCalendarDateDateOfMonth to the second word of theCalendarDate
set thePassage to formatted value of cell i of column "B"
set thePageNumbers to formatted value of cell i of column "C"
set the end of CalendarDates to theCalendarDate
set the end of CalendarDateMonths to theCalendarDateMonth
set the end of CalendarDateDatesOfMonths to theCalendarDateDateOfMonth
set the end of Passages to thePassage
set the end of PageNumbers to thePageNumbers
end repeat
set theCalendarDate to missing value
set theCalendarDateMonth to missing value
set theCalendarDateDateOfMonth to missing value
set thePassage to missing value
set thePageNumbers to missing value
end tell
repeat with i from 1 to the count of CalendarDates
set theSummary to (item i of Passages)
set theStartDate to makeASDate given |year|:theYear, |month|:(item i of CalendarDateMonths), |day|:(item i of CalendarDateDatesOfMonths), |hours|:7, |minutes|:30
set theEndDate to makeASDate given |year|:theYear, |month|:(item i of CalendarDateMonths), |day|:(item i of CalendarDateDatesOfMonths), |hours|:8, |minutes|:30
set theDescription to "Pages " & (item i of PageNumbers) & "of Tyndale's The Chronological Life Application Study Bible."
set theURL to "https://www.biblegateway.com/passage/?search=" & urlEncode(item i of Passages) & "&version=AMP&interface=print"
set theAlldayEvent to false
set theStampDate to current date
set theStatus to "none"
set theLocation to "facetime:[email protected]"
tell application "Calendar"
activate
-- If calendar doesn't exist, create such. Note, this creates local. I don't yet know how to create an iCloud-based Calendar, yet iCloud calendars may be selected if pre-created.
set theCalendarName to "Bible Readings"
try
set theCalendar to (first calendar where its name = theCalendarName)
on error
set theCalendarDescription to "Readings for the reading of the Bible, chronologically, in the span of one year."
set theCalendar to make new calendar with properties {name:theCalendarName, description:theCalendarDescription}
end try
-- Ensure work is with the proper calendar.
set theCalendar to (first calendar where its name = theCalendarName)
tell calendar theCalendar
make new event with properties {description:theDescription, start date:theStartDate, end date:theEndDate, theAlldayEvent:false, stamp date:current date, status:none, summary:theSummary, location:theLocation, url:"http://biblegateway.com/"}
end tell
end tell
end repeat
The Numbers worksheet is loading alright, yet on constructing the date with MakeASDate, I receive the error "The year parameter is missing for makeASDate."
Any assistance in correcting what's wrong would be helpful!
Edit: I have updated the code to:
set theStartDate to makeASDate given year:2021, month:(item i of CalendarDateMonths as integer), day:(item i of CalendarDateDatesOfMonths as integer), hours:7, minutes:30, seconds:0
set theEndDate to makeASDate given year:2021, month:(item i of CalendarDateMonths as integer), day:(item i of CalendarDateDatesOfMonths as integer), hours:8, minutes:30, seconds:0
display dialog "CalendarDateMonths[" & i & "]" & ": " & (item i of CalendarDateMonths as integer) & linefeed & linefeed & "theStartDate: " & theStartDate & linefeed & linefeed & "theEndDate: " & theEndDate
Now when I compile and run the code, it returns March instead of February when I enter a value of "2" (I changed the format of the Date column in numbers to be m/d):
The handler from Apple Script: Can’t get date of "2018-12-12 10:00 AM" is buggy: its results depend on the current date. See the full explanation there, but the short version is that when setting a component of an AppleScript date
, if you create an impossible date such as February 31, it will adjust to create a possible one: in this case, March 3 (or March 2 in a leap year).
To fix the handler, either set both the day
and month
to 1 first, or start with a fixed date with a low-numbered month and day such as "1/1/1970" instead of current date
.