title: How do I add an iCal event using JS automation in OS X Yosemite?
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>Thanks</p>
  
  <p><em>UPDATE</em></p>
  
  <p>Here is the code I am trying..</p>
  
  <pre><code>// -- 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);
  
  }
  </code></pre>
  
  <p>The error i'm receiving.. </p>
  
  <pre><code>Error on line 68: TypeError: undefined is not an object (evaluating 'events.push(theEvent)')
  </code></pre>
  
  <p><em>UPDATE</em></p>
  
  <p>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 </p>
  
  <pre><code>events[last] = theEvent;
  </code></pre>