copying calendar into existing one

I would like to

  • delete all events in Calendar X (“destination calendar”)
  • copy all events from Calendar Y (“source calendar”) into Calendar X

The code below has worked until recently. I am not sure what caused it to fail (update to 10.15.7 from 10.15.6?)

Now, when it attempts to copy the first event, it throws the error

Error: Events can not be copied. (-1717)

Notes:

- this happens with iCloud or exchange or other calendars. 
- this happens with copy or duplicate events
- when it was working I had the calendars referenced via their UIDs (see comment in code); with the UIDs enabled, I get the error "Error: the Apple Event handler failed (errAEEventFailed:-10000)"

Questions:
- why might copying/duplicating events be problematic? Is there another way to do this? I want to copy the events into an existing calendar for sharing reasons (I cannot share the source calendar)
- Is there a way to find out more details when this error occurs? What does “-1717” imply?
- when copying fails, might this be due to a lack of read/write permissions? Is this a new “security feature”?

Thanks for your help!


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

-- destination ist der posteo Kalender "Claus_Work_Copy"
-- set destinationUid to "B0E49013-1183-4E70-84BC-C56A2157E2BD"
set DestinationCalendarName to "egon"

-- sources ist der IWS LHG Calendar
-- set sources to {"BF607163-CA76-45C5-AB2F-7437B47BC9DA"}
set SourceCalendarName to "Calendar"


tell application "Calendar"
	-- set destinationCalendar to first calendar where its uid = destinationUid
	-- set sourceCalendar to (first calendar where its uid = sourceUid)
	set destinationCalendar to (first calendar where its name = DestinationCalendarName)
	set sourceCalendar to (first calendar where its name = SourceCalendarName)
	
	-- Clear the destination calendar
	repeat with anEvent in (get events of destinationCalendar)
		delete anEvent
	end repeat
	-- If this loop displays anything the clearing didn't work
	repeat with anEvent in (get events of destinationCalendar)
		display dialog "Failed to clear " & summary of anEvent as string
	end repeat
	
	-- Copy sources to destination --
	repeat with anEvent in (get events of sourceCalendar)
		try
			copy anEvent to the end of events of destinationCalendar
			-- duplicate anEvent to end of destinationCalendar -- with properties {description:"Copied Automatically"}
		end try
	end repeat
	repeat with anEvent in (get events of destinationCalendar)
		display dialog summary of anEvent as string
	end repeat
end tell

Model: Mac Mini 2018
Browser: Safari 605.1.15
Operating System: macOS 10.14

I don’t know. Some properties of source event you can’t set as property of new created event. For example, the UID of certain event is unique (for it only). I guess this is the reason why you got error when trying to copy all properties of event together. For me duplicating events work fine this way:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set SourceCalendarName to "Calendar"
set DestinationCalendarName to "egon"

tell application "Calendar"
	
	set sourceCalendar to calendar SourceCalendarName
	set destinationCalendar to calendar DestinationCalendarName
	
	-- Clear the destination calendar
	delete events of destinationCalendar
	
	-- Copy sources to destination --
	repeat with anEvent in (get events of sourceCalendar)
		
		tell anEvent
			set |allday event| to allday event
			set |start date| to start date
			set |end date| to end date
			set |url| to url
			set |location| to location
			set |recurrence| to recurrence
			set |sequence| to sequence
			set restProperties to {description, excluded dates, stamp date, summary}
		end tell
		
		tell destinationCalendar
			set newEvent to (make new event at end of events with properties {allday event:|allday event|, start date:|start date|, end date:|end date|})
			tell newEvent
				if not (|url| is missing value) then set url to |url|
				if not (|location| is missing value) then set location to |location|
				if not (|recurrence| is missing value) then set recurrence to |recurrence|
				if not (|sequence| is 0) then set sequence to |sequence|
				set {description, excluded dates, stamp date, summary} to restProperties
			end tell
		end tell
		
	end repeat
end tell

Thanks, KniazidisR, your script works (mostly)!

A couple of thoughts:

  • why do you include description, excluded dates, stamp date and summary into one variable called restProperties?.. in my case the script sometimes failed when one of them was wrong (e.g., missing)
  • so I resorted to setting each item individually, which mostly works (sometimes description (the title) is missing (likely due to “weird” characters).

Thanks again!

(Link corrected by NG.)

You are right.
I checked the Calendar dictionary. From there we can see that:

  1. some properties may not have initial values (they are undefined initially)
  2. the UID and sequence are read/only.

That is, the 1) should be checked before copying, the 2) should not be touched. Corrected script:


set SourceCalendarName to "Calendar"
set DestinationCalendarName to "egon"

tell application "Calendar"
	
	set sourceCalendar to calendar SourceCalendarName
	set destinationCalendar to calendar DestinationCalendarName
	
	-- Clear the destination calendar
	delete events of destinationCalendar
	
	-- Copy sources to destination --
	repeat with anEvent in (get events of sourceCalendar)
		
		tell anEvent
			set |allday event| to allday event
			set |start date| to start date
			set |end date| to end date
			set |url| to url
			set |location| to location
			set |recurrence| to recurrence
			set |description| to description
			set |excluded dates| to excluded dates
			set |stamp date| to stamp date
			set |summary| to summary
		end tell
		
		tell destinationCalendar
			set newEvent to (make new event at end of events with properties {allday event:|allday event|, start date:|start date|, end date:|end date|})
			tell newEvent
				if not (|url| is missing value) then set url to |url|
				if not (|location| is missing value) then set location to |location|
				if not (|recurrence| is missing value) then set recurrence to |recurrence|
				if not (|description| is missing value) then set description to |description|
				if not (|summary| is missing value) then set summary to |summary|
				if not (|excluded dates| is missing value) then set excluded dates to |excluded dates|
				if not (|stamp date| is missing value) then set stamp date to |stamp date|
			end tell
		end tell
		
	end repeat
end tell