Monday, November 25, 2024

Applescript – Find if the calendar event already exists

I am trying to create a script that copies events from my work calendar to a personal but just as time blocks so that my spouse knows when I have meetings. From scouring the internet for a bit I came up with the following script which works for the most part.

set SourceCalendarName to "Work"
set DestinationCalendarName to "Mirror"
set today to current date

set numberofdays to 7
set startday to ((current date) - (1 * days))
set endday to ((current date) + (numberofdays * days))

tell application "Calendar"
    
    set sourceCalendar to calendar SourceCalendarName
    set destinationCalendar to calendar DestinationCalendarName
    
    
    -- Copy sources to destination --
    repeat with sourceEvent in (get events of sourceCalendar whose (start date is greater than or equal to startday) and (start date is less than endday))
        
        
        tell sourceEvent
            set |allday event| to allday event
            set |start date| to start date
            set |end date| to end date
            set |recurrence| to recurrence
            set |description| to description
            set |summary| to summary
            set |status| to status
            set |summary| to "VV Meeting"
            
        end tell
        
        
        delete (events of destinationCalendar whose start date is equal to |start date| and end date is equal to |end date|)
        tell destinationCalendar
            set destEvent to (make new event at end of events with properties {start date:|start date|, end date:|end date|, summary:|summary|})
        end tell
        
    end repeat
end tell

I am trying to not have the delete events part but check if there is an event already and not bother creating an event in the first place

delete (events of destinationCalendar whose start date is equal to |start date| and end date is equal to |end date|)

Any pointers on how to change this to an if-condition so that the tell even can be optional?

Thank you

Related Articles

Latest Articles