Why is an AppleScript to modify Calendar event attributes so slow

applescriptcalendar

I'm trying to write a fairly simple AppleScript to automate the process of having Calendar events repeat in bulk. Specifically, I want every event on a given calendar within a given time span to have its repetition set to be biweekly.

This seems like a fairly routine and quick task, but the script below, when run on approximately 30 events in a one-week span, takes about an hour and a half to complete. Moreover, this is only true if I leave the computer untouched while the script runs; if I try to use the laptop simultaneously, the script eventually fails with an error indicating that an Apple Event timed out.

Indeed, iterating through calendar events and accessing or modifying any of their properties seems to take an astronomically long time in AppleScript (for instance, just calling log on the summary of each event). I've tried moving the event fetch to a separate variable and using attributes other than event dates to fetch the events, but nothing seems to improve performance.

What is the reason for this incredible slowness, and what can be done to alleviate it? Or is this simply an inherent part of AppleScript/the macOS Scripting Bridge/Calendar itself?

Here is the script in question:

tell application "Calendar"
    set cal to calendar 1 whose name is "Schedule"
    set theStartDate to date "Sunday, January 27, 2019 at 00:00:00"
    set theEndDate to date "Saturday, February 2, 2019 at 00:00:00"
    repeat with e in (every event of cal whose start date is greater than theStartDate and start date is less than theEndDate)
        set esStartDate to e's start date
        set theWeekday to weekday of esStartDate
        set lower to text 1 thru 2 of (theWeekday as string)
        set upper to do shell script "echo " & lower & " | tr [:lower:] [:upper:]"
        set e's recurrence to "FREQ=WEEKLY;INTERVAL=2;UNTIL=20190602T035959Z;BYDAY=" & upper & ";WKST=SU"
    end repeat
end tell

Looking at the "Replies" console, the script slows both on the initial count every event of calendar id "[ID]" Apple Event as well as on each attempt to modify an event's recurrence.

I'm running macOS Mojave 10.14.2 (Calendar 11.0) on a MacBook Pro early 2015.

Best Answer

Use CalendarLib. CalendarLib is a Script Library that lets you work with Calendar events without using the Calendar app. It is very, very fast:

CalendarLib

CalendarLib is an AppleScript script library for manipulating calendar events without using Calendar.app. It requires OS X 10.9 or later, as well as the BridgePlus script library.

I do not know why Calendar is so slow with AppleScript.

Related Question