Skip to content

Instantly share code, notes, and snippets.

@rbreaves
Created June 30, 2023 18:19
Show Gist options
  • Save rbreaves/2c85548846043bdd00e9e45c6eb60c7e to your computer and use it in GitHub Desktop.
Save rbreaves/2c85548846043bdd00e9e45c6eb60c7e to your computer and use it in GitHub Desktop.
macOS Clone Calendars
-- list all calendars in order they appear in Calendar app, w/ name and uid
-- this works in Catalina & up, a bug breaks the old uid param.
on identifyCalendars()
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"Can’t make «class wres» id \"", "\" of application \"Calendar\" into type text."}
tell application "Calendar"
set calendarList to {}
repeat with aCalendar in calendars
try
set end of calendarList to {uid of aCalendar, name of aCalendar}
on error number -10000 -- error "AppleEvent handler failed"
try
aCalendar as text
on error errorMessage
set theID to text item 2 of errorMessage
set end of calendarList to {theID, name of aCalendar}
end try
end try
end repeat
end tell
set AppleScript's text item delimiters to ATID
return calendarList
end identifyCalendars
identifyCalendars()
set SourceCalendarUID to "77802B67-AA8B-4D58-B514-C04B876DD8D7"
-- If you manually set the source name you can re-enable this
-- and one other place below
--set SourceCalendarName to "CalendarName"
set DestinationCalendarName to "Meetings"
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"Can’t make «class wres» id \"", "\" of application \"Calendar\" into type text."}
tell application "Calendar"
-- this is needed if you need to pull your source from uid
-- due to mutliple matching names
set allCalendars to every calendar
set sourceCalendar to missing value
-- Find the source calendar by UID
repeat with aCalendar in allCalendars
try
set theID to uid of aCalendar
on error number -10000 -- error "AppleEvent handler failed"
try
aCalendar as text
on error errorMessage
set theID to text item 2 of errorMessage
end try
end try
if theID is SourceCalendarUID then
set sourceCalendar to aCalendar
exit repeat
end if
end repeat
-- If we couldn't find the source calendar, exit
if sourceCalendar is missing value then error "Could not find source calendar"
-- end source uid part
-- If you manually set the source name you can re-enable this
-- 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
set AppleScript's text item delimiters to ATID
end tell
@rbreaves
Copy link
Author

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