1

I'm really having a hard time understanding how to interact with iCal using the new support for Javascript automation in Yosemite. I'm not too experienced with OOP so that might be where my problem lies.

Could someone help me out with the code for adding a new event to a specified calendar? I can probably reverse engineer it from there to understand how to work with it.

Thanks

UPDATE

Here is the code I am trying..

// -- perform adding to iCal

// -- select "Test" calendar
theCalendar = iCal.calendars.Test;
events = theCalendar.events;

// -- load in the events
stringifiedData = Safari.doJavaScript("localStorage.getItem('theEvents');", {
in: Safari.windows[0].tabs[0]})

// -- parse data back to js object
theEvents = JSON.parse(stringifiedData);

// -- loop through events
for (var key in theEvents) {
    var obj = theEvents[key];

    // -- set vars
    theDate = obj['date'];
    theDescription = obj['description'];
    theSummary = obj['summary'];
    theLocation = obj['location'];
    theStartTime = theDate + " " + obj['startTime'];
    theEndTime = theDate + " " + obj['endTime'];

    // -- create event object
    theEvent = iCal.Event({

        description: theDescription,
        summary: theSummary,
        location: theLocation,
        startDate: theStartTime,
        endDate: theEndTime

    });

    // -- get the last index event
    last = events.length;

    // -- insert it into iCal
    events.push(theEvent);

}

The error i'm receiving..

Error on line 68: TypeError: undefined is not an object (evaluating 'events.push(theEvent)')

UPDATE

I figured out the issue. I was formatting the dates incorrectly.. they were just strings. So I made sure to turn them into date objects, and I was able to insert successfully using

events[last] = theEvent;
Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
Brian Peters
  • 297
  • 1
  • 3
  • 17
  • 1. Application scripting isn't OOP, it's a combination of remote procedure calls with simple first-class relational queries. Trying to squash it into a rigid OO mindset will only confuse and frustrate. 2. Find an existing AppleScript example, and use that as a starting point, manually translating it to JS. (I recommended the JXA team include a simple auto-translate tool, but they ignored me.) 3. Be aware that JXA suffers crippled functionality, application incompatibilities, and poor, misleading documentation. AppleScript remains the only supported option that does application scripting right. – foo Oct 24 '14 at 09:18
  • My only reason of using it is because I need to send a javascript object and use it in applescript. Basically I have an array of objects that I want to be able to iterate through in applescript. I send it from safari using encoded URL data or I can also use localStorage. I'll try what you mentioned, however do you know of anyways of parsing an array of objects into applescript? I would have to stringify it.. but I believe that only supports objects and not arrays, or vice versa. – Brian Peters Oct 24 '14 at 15:04
  • You should post any code/examples you've already got to clarify what you're trying to do, where the iCal part fits in, and how you're passing data between Safari and AS/JXA. e.g. It's not clear if you're calling Safari from AS/JXA via `do JavaScript`, or calling AS/JXA from a Safari web page via a custom URL scheme handler. – foo Oct 24 '14 at 20:25
  • disregard my object passing problems, figured that out as you can see from the code above. – Brian Peters Oct 24 '14 at 22:51

0 Answers0