Documentation Archive

Developer

Calendar Scripting Guide

On This Page

Locating an Event

There are many ways to locate an event through scripting. The most accurate way is to match an event to a unique identifier. Listing 9-1 and Listing 9-2 look for an event with a specific uid property value. This method assumes you know the uid for the event.

APPLESCRIPT

Open in Script Editor

Listing 9-1AppleScript: Locating an event by its ID
  1. tell application "Calendar"
  2. tell calendar "Project Calendar"
  3. first event where its uid = "538E181E-7043-45A5-8F61-4711724F1A1B"
  4. end tell
  5. end tell
  6. --> Result: event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"

JAVASCRIPT

Open in Script Editor

Listing 9-2JavaScript: Locating an event by its ID
  1. var app = Application.currentApplication()
  2. var Calendar = Application("Calendar")
  3. var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
  4. var projectCalendar = projectCalendars[0]
  5. var event = projectCalendar.events.byId("DD524F6C-A48B-4841-B086-A8891ED84C8D")
  6. event
  7. // Result: Application("Calendar").calendars.whose({_match: [ObjectSpecifier().name, "Project Calendar"]}).calendars.at(0).events.byId("DD524F6C-A48B-4841-B086-A8891ED84C8D")

Locating an Event by Name

Another way to locate an event is by name. The summary property value contains the name of the event as it appears on the calendar. Listing 9-3 and Listing 9-4 look for an event with a summary property value of "Important Meeting!". A disadvantage of this method is that event summaries aren’t unique. Therefore, multiple events may exist with the same summary, making it difficult to identify a specific one.

APPLESCRIPT

Open in Script Editor

Listing 9-3AppleScript: Locating an event by its summary
  1. tell application "Calendar"
  2. tell calendar "Project Calendar"
  3. first event where its summary = "Important Meeting!"
  4. end tell
  5. end tell
  6. --> Result: event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"

JAVASCRIPT

Open in Script Editor

Listing 9-4JavaScript: Locating an event by its summary
  1. var app = Application.currentApplication()
  2. var Calendar = Application("Calendar")
  3. var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
  4. var projectCalendar = projectCalendars[0]
  5. var events = projectCalendar.events.whose({summary: "Important Meeting!"})
  6. var event = events[0]
  7. event
  8. // Result: Application("Calendar").calendars.whose({_match: [ObjectSpecifier().name, "Project Calendar"]}).calendars.at(0).events.whose({_match: [ObjectSpecifier().summary, "Important Meeting!"]}).events.at(0)

Locating an Event by Date

You can also locate events by date. Start dates and end dates also include times, so if you’re looking for an event that falls on a specific date, make sure you search for events within a specific timeframe. Listing 9-5 and Listing 9-6 demonstrate this technique by looking for any events with a start date and an end date between 12 a.m. today and 12 a.m. tomorrow.

APPLESCRIPT

Open in Script Editor

Listing 9-5AppleScript: Locating events by its date
  1. set theStartDate to current date
  2. set hours of theStartDate to 0
  3. set minutes of theStartDate to 0
  4. set seconds of theStartDate to 0
  5. set theEndDate to theStartDate + (1 * days) - 1
  6. tell application "Calendar"
  7. tell calendar "Project Calendar"
  8. every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate
  9. end tell
  10. end tell
  11. --> Result: {event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"}

JAVASCRIPT

Open in Script Editor

Listing 9-6JavaScript: Locating events by date
  1. var app = Application.currentApplication()
  2. app.includeStandardAdditions = true
  3. var Calendar = Application("Calendar")
  4. var startDate = app.currentDate()
  5. startDate = startDate
  6. startDate.setHours(0)
  7. startDate.setMinutes(0)
  8. startDate.setSeconds(0)
  9. var endDate = app.currentDate()
  10. endDate.setHours(23)
  11. endDate.setMinutes(59)
  12. endDate.setSeconds(59)
  13. var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
  14. var projectCalendar = projectCalendars[0]
  15. var events = projectCalendar.events.whose({startDate: {_greaterThan: startDate}, endDate: {_lessThanEquals: endDate}})
  16. var event = events[0]
  17. event
  18. // Result: Application("Calendar").calendars.whose({_match: [ObjectSpecifier().name, "Project Calendar"]}).calendars.at(0).events.whose({_and: [{_match: [ObjectSpecifier().startDate, {">": Wed Nov 18 2015 00:00:00 GMT-0800 (PST)}]}, {_match: [ObjectSpecifier().endDate, {"<": Wed Nov 18 2015 23:59:59 GMT-0800 (PST)}]}]}).events.at(0)