Sunday, August 10, 2025

calendar – AppleScript – Find Recurring events within date range

Looks like one needs to use CalendarLibEC from Shane (https://latenightsw.com/freeware/)

use script "CalendarLib EC" version "1.1.5"
use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions


-- Change variables below
set SourceCalendarName to "VV Main"
set DestinationCalendarName to "VV Mirror"
set meetingProxy to "VV Meeting"
set addRecurringEvents to false
set numberofdays to 7

-- Changes should not be required below, but proceed at your own risk

set today to current date

set startDay to (today)
set time of startDay to 0
set endDay to (startDay + (numberofdays * days))

set numberOfEventsAdded to 0
set numberofEventsFailedtoCopy to 0

set theStore to («event !Cls!fst»)
-- The original script only compares events' start and end dates.
set destCal to («event !CLs!fca» DestinationCalendarName given «class !Cty»:«constant !Tct!TtC», «class !Cst»:theStore) -- change calendar type to suit
set existingEventList to («event !CLs!feS» given «class !Csd»:startDay, «class !Ced»:endDay, «class !Csc»:{destCal}, «class !Cst»:theStore)

repeat with thisEvent in existingEventList
    set {event_start_date:startDate, event_end_date:endDate} to («event !CLs!inf» given «class !Cev»:thisEvent)
    set thisEvent's contents to {startDate, endDate}
end repeat

set existingEventDates to existingEventList -- Use a different variable now for clarity.

-- Get the source calendar events which fall within the same date range.
set sourceCal to («event !CLs!fca» SourceCalendarName given «class !Cty»:«constant !Tct!TtC», «class !Cst»:theStore) -- change to suit
set sourceEventList to («event !CLs!feS» given «class !Csd»:startDay, «class !Ced»:endDay, «class !Csc»:{sourceCal}, «class !Cst»:theStore)
repeat with eventIdx from 1 to length of sourceEventList
    set newEvent to item eventIdx of sourceEventList
    -- Get the data of interest for each source event.\
    try
        set {event_external_ID:eventID, event_start_date:startDate, event_end_date:endDate, all_day:isAllday, event_is_recurring:isRecurring} to («event !CLs!inf» given «class !Cev»:newEvent)
        
        -- If its start and end dates aren't in existingEventDates, create a new event using the Calendar appplication..
        if (existingEventDates does not contain {{startDate, endDate}}) then
            
            tell application "Calendar"
                set destEvent to (make new event at end of calendar DestinationCalendarName's events with properties {start date:startDate, end date:endDate, summary:meetingProxy, allday event:isAllday})
                
                if addRecurringEvents is true then
                    if (isRecurring) then
                        set destEvent's recurrence to recurrence of event id eventID of calendar SourceCalendarName
                    end if
                end if
                
                set end of existingEventDates to {startDate, endDate} -- if required
                set numberOfEventsAdded to numberOfEventsAdded + 1
            end tell
            
        end if
        
    on error errMsg number -10000 --*** -L_NSDictionaryM setObject:forKey:]: object cannot be nil (key: event_creation_date) 
        set eventIdx to {eventIdx + 1}
        set numberofEventsFailedtoCopy to (numberofEventsFailedtoCopy + 1)
        
    end try
    
end repeat

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles