Search code examples
javascriptmacosapplescriptjavascript-automationosx-yosemite-beta

JavaScript for Automation - Searching Calendar


In OSX Yosemite I am use the new JavaScript for Automation feature to script Calendar. I am attempting to search a Calendar to see if there is an event that is currently occurring.

I have done this in AppleScript like so:

set now to current date
every event whose start date < now and end date > now

I notice the in AppleScript there is a specialized AppleScript 'date' object. The event log displays like below. Special note about the "date" modifier before the string, indicating that this is a date object and not just a string.

count every event whose start date < date "Monday, August 11, 2014 at 5:49:46 PM" and end date > date "Monday, August 11, 2014 at 5:49:46 PM" and allday event = false

I am attempting to do this in AppleScript like so:

workCalendar.events.whose({
    _and: [
        { startDate: {'<': now} },
        { endDate: {'>': now} }
    ]
 });

But any value I set for the now var gives me the following error:

Error -1700: Can't convert types.

I have tried the following values for now and all return type conversion errors:

calendarApp.includeStandardAdditions = true;
now = calendarApp.currentDate;

now = (new Date());

now = (new Date()).toISOString();

now = $.NSDate.date;

Try to figure out what type of value it could be I did this:

firstStartDate = workCalendar.events[0].startDate();

console.log("firstStartDate : " + firstStartDate);
console.log("firstStartDate TO : " + (typeof firstStartDate));
console.log("firstStartDate OS : " + ObjectSpecifier.classOf(firstStartDate));

Which results in this inside the event log:

/* firstStartDate : Wed Jun 18 2014 10:30:00 GMT-0700 (PDT) */
/* firstStartDate TO : object */
/* firstStartDate OS : undefined */

Solution

  • Apple acknowledged this is a bug - as of Yosemite Public Beta 3 this works:

    now = (new Date());
    

    And this works

    firstStartDate = workCalendar.events[0].startDate();
    

    And this works

        var currentApp = Application.currentApplication();
        currentApp.includeStandardAdditions = true;
        now = currentApp.currentDate();