1

In Javascript for Automation, one can create an element object and then add it to a container. For instance:

    var cal = Application('Calendar')
    var newEvent = cal.Event(
          {    
               summary: todoSummary,
               startDate: new Date(),
               endDate: endDate
          }
    );
    cal.calendars[0].events.push(newEvent);
}

This method of working is nicely described and documented in many places, such that Calendar (iCal) does not need to have its own methods for doing this.

How though does one remove an object (Event, etc.) from the container (events), or at least delete the relationship between this event and its calendar? In AppleScript one would write:

delete (every event whose uid is eventID)

So it would seem that the JXA version would be something like:

cal.calendars.events.delete.whose({uid: event.uid()})

But various attempts just give me invalid key forms or Can't convert types errors. Thanks!

1 Answers1

2

It turns out that the delete method resides on the Application object itself, so, it was as simple as:

// delete most recent message on first calendar
var ical = Application('Calendar');
var ev = ical.calendars[0].events.last();
ical.delete(ev);

// and specifically for OS X calendar:
ical.reloadCalendars();
  • did you find you sometimes got " Error on line 48: TypeError: Calendar.delete is not a function. (In 'Calendar.delete(recentEvent())', 'Calendar.delete' is undefined)" – OrigamiEye Mar 18 '20 at 13:49
  • I didn't, but I haven't been running this script for a couple of years. – Michael Scott Asato Cuthbert Mar 21 '20 at 16:22
  • Thanks for answering, for future searchers I will share my fix: I was trying to delete an event from a result of a .whose search. This worked sometimes. Now I get Id of the event from the search result and delete by id instead ```ical.delete(ical.calendars[0].events.byId(searchResult[0].id()));```. This works every time. – OrigamiEye Mar 22 '20 at 10:00