Skip to content

Instantly share code, notes, and snippets.

@pwc3
Last active February 1, 2024 12:34
Show Gist options
  • Save pwc3/5342337 to your computer and use it in GitHub Desktop.
Save pwc3/5342337 to your computer and use it in GitHub Desktop.
AppleScript to copy all of the events from an Exchange calendar to an iCloud calendar.
tell application "Calendar"
-- delete everything from the destination calendar
-- TODO: Change "Destination Calendar" to be the name of your destination calendar
repeat with anEvent in (get events of calendar "Destination Calendar")
delete anEvent
end repeat
-- copy all events from the source calendar to the destination
-- TODO: Change "Source Calendar" to be the name of your source calendar
-- TODO: Change "Destination Calendar" to be the name of your destination calendar
repeat with anEvent in (get events of calendar "Source Calendar")
copy anEvent to the end of events of calendar "Destination Calendar"
end repeat
end tell
@tnacsak
Copy link

tnacsak commented Mar 10, 2020

Hi!
I played some hour with the topic at it has some infuriating problems.

  • First, AppleScript drops an error of missing handler for uids of calendars, and many properties of the events (url, allday event, location and so).
  • The other problem is recurrence - it was impossible to copy from an Exchange calendar to an icloud cal. It is a string, but the string from one event as a copy to the new event raises 1700 error code (Bad parameter data was detected or there was a failure while performing a coercion.)
  • Other issue is the fields with missing value, you see the code

I hope you can use the code below, but it is far to be perfect. I also planned to have a script which list the calendars and their uids to make the code target really the one you need, but the missing handler made it impossible.

set theStartDate to (current date) - (20 * days)
set hours of theStartDate to 0
set minutes of theStartDate to 0
set seconds of theStartDate to 0
set theEndDate to theStartDate + (14 * days) - 1

set sourceCalendarTitle to "YourSourceCalendarTitle"
set destinationCalendarTitle to "YourDestinationCalendarTitle"

tell application "Calendar"
	set destinationCalendar to (first calendar where its title = destinationCalendarTitle)
	set sourceCalendar to (first calendar where its title = sourceCalendarTitle)
	
	tell destinationCalendar
		set calendarEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate)
		set numberOfEvents to count calendarEvents
		repeat with i from 1 to numberOfEvents
			set anEvent to item i of calendarEvents
			delete anEvent
		end repeat
	end tell
	
	tell sourceCalendar
		set sourceEvents to (every event where its start date is greater than or equal to theStartDate and end date is less than or equal to theEndDate)
		repeat with i from 1 to count sourceEvents
			set anEvent to item i of sourceEvents
			set tmpDescription to description of anEvent
			if tmpDescription = missing value then
				set tmpDescription to ""
			end if
			
			if recurrence of anEvent = missing value then
				make new event at destinationCalendar with properties {summary:summary of anEvent, start date:start date of anEvent, end date:end date of anEvent, description:tmpDescription, allday event:allday event of anEvent}
			end if
		end repeat
	end tell
end tell

@masciugo
Copy link

it works with BigSur on mbp M1. thank you

@masciugo
Copy link

... the problem is that it misses recurring events whose first event does not happen inside the selected time window

@alexdd55
Copy link

alexdd55 commented Jul 5, 2021

sadly non of this works for me (Big Sur, MBP M1)

@MyKEms
Copy link

MyKEms commented Feb 1, 2024

Hi, I needed this feature, so I created an updated version for anyone interested (compatible with the current macOS version): https://gist.github.com/MyKEms/3287c65f097a29b1756f3799842165bb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment