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
tell application "Calendar"
tell calendar "Project Calendar"
first event where its uid = "538E181E-7043-45A5-8F61-4711724F1A1B"
end tell
end tell
--> Result: event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"
JAVASCRIPT
var app = Application.currentApplication()
var Calendar = Application("Calendar")
var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
var projectCalendar = projectCalendars[0]
var event = projectCalendar.events.byId("DD524F6C-A48B-4841-B086-A8891ED84C8D")
event
// 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
tell application "Calendar"
tell calendar "Project Calendar"
first event where its summary = "Important Meeting!"
end tell
end tell
--> Result: event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"
JAVASCRIPT
var app = Application.currentApplication()
var Calendar = Application("Calendar")
var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
var projectCalendar = projectCalendars[0]
var events = projectCalendar.events.whose({summary: "Important Meeting!"})
var event = events[0]
event
// 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
set theStartDate to current date
set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (1 * days) - 1
tell application "Calendar"
tell calendar "Project Calendar"
every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate
end tell
end tell
--> Result: {event id "538E181E-7043-45A5-8F61-4711724F1A1B" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"}
JAVASCRIPT
var app = Application.currentApplication()
app.includeStandardAdditions = true
var Calendar = Application("Calendar")
var startDate = app.currentDate()
startDate = startDate
startDate.setHours(0)
startDate.setMinutes(0)
startDate.setSeconds(0)
var endDate = app.currentDate()
endDate.setHours(23)
endDate.setMinutes(59)
endDate.setSeconds(59)
var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
var projectCalendar = projectCalendars[0]
var events = projectCalendar.events.whose({startDate: {_greaterThan: startDate}, endDate: {_lessThanEquals: endDate}})
var event = events[0]
event
// 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)
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-09-13