Guides and Sample Code

Developer

Calendar Scripting Guide

Creating an Event

Use the make command to create events on a given calendar. Listing 8-1 and Listing 8-2 demonstrate how to do this by creating a 1-hour meeting event, tomorrow, on a project calendar. The result of each example is a reference to the newly created event.

APPLESCRIPT

Open in Script Editor

Listing 8-1AppleScript: Creating a new event
  1. set theStartDate to (current date) + (1 * days)
  2. set hours of theStartDate to 15
  3. set minutes of theStartDate to 0
  4. set seconds of theStartDate to 0
  5. set theEndDate to theStartDate + (1 * hours)
  6. tell application "Calendar"
  7. tell calendar "Project Calendar"
  8. make new event with properties {summary:"Important Meeting!", start date:theStartDate, end date:theEndDate}
  9. end tell
  10. end tell
  11. --> Result: event id "10D0A87A-3B92-474B-8365-D5434280AA31" of calendar id "CDF2EA89-AE82-44C0-B1B6-449128A5E151" of application "Calendar"

JAVASCRIPT

Open in Script Editor

Listing 8-2JavaScript: Creating a new event
  1. var app = Application.currentApplication()
  2. app.includeStandardAdditions = true
  3. var Calendar = Application("Calendar")
  4. var eventStart = app.currentDate()
  5. eventStart = eventStart
  6. eventStart.setDate(eventStart.getDate() + 1)
  7. eventStart.setHours(15)
  8. eventStart.setMinutes(0)
  9. eventStart.setSeconds(0)
  10. var eventEnd = new Date(eventStart.getTime())
  11. eventEnd.setHours(16)
  12. var projectCalendars = Calendar.calendars.whose({name: "Project Calendar"})
  13. var projectCalendar = projectCalendars[0]
  14. var event = Calendar.Event({summary: "Important Meeting!", startDate: eventStart, endDate: eventEnd})
  15. projectCalendar.events.push(event)
  16. event
  17. // Result: Application("Calendar").calendars.whose({_match: [ObjectSpecifier().name, "Project Calendar"]}).calendars.at(0).events.byId("D7D2AC03-C5DF-4415-B6E3-0243E0314808")