0

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.

Data from Numbers

From https://apple.stackexchange.com/questions/345480/apple-script-can-t-get-date-of-2018-12-12-1000-am.

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:chrishota@gmail.com"
    
    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."

Script Debugger showing a runtime error in the makeASDate handler.

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 value supplied is 2, yet March is returned by the makeASDate handler.

15
  • You are not properly using the MakeASDate handler! It requires the use of valid values for date objects and when you wrap it with | (pipe character) a date object, e.g. year, like |year| it is no longer a date object but a string. Dec 31 '20 at 1:13
  • Thanks for the tip; I changed my code and edited the question to reflect that now, makeASDate is returning "March" when 2 is supplied, rather than "February." Perhaps you could post some insight? Dec 31 '20 at 3:06
  • This issue of your original OP was easy to spot as you were not properly using the MakeASDate handler, however, as to your edited OP and the new issue you have, sorry but I do not waste time trying to debug someone's code when they do not provide all the necessary information! Without access to the Numbers document, how do you expect us to debug your code? Dec 31 '20 at 3:27
  • Great! By all means, submit an answer that I may accept it. I recommend you ("user3439894") spend some time cultivating a helpful attitude of servant-leadership (or at least a welcoming one, as the "New contributor" badge is clearly displayed). Surely that's no waste. Also, the use of italics in the comments thus far is difficult to understand. Is there some way I can give you "points" or something for correctly answering the original problem? I don't intend to "move the goalposts." Is that the reason your responses are acerbic? Dec 31 '20 at 3:57
  • That's an odd one. I get the wrong month as well unless it's January. Odder still, if I pick a different month and then subtract 1, the result is one month before the correct month.
    – Mockman
    Dec 31 '20 at 6:05
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .