Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Copies events between calendars in Apple
-- Syncronize several calendars
-- The calendars to be synced must be set by UID in the 'targetCalendarIDs' variable.
-- UIDs can be read using the 'Get Calendar' script.
-- When run, copies each selected event to other calendars in the target list.
-- Author: Spencer Bliven (
-- Event selection code from
-- Known Limitations:
-- Running the script multiple times produces duplicate events.
on main()
--display dialog "Starting script..."
set eventReferenceList to my selectedEvents()
set targetCalendarIDs to {"B8D760C1-73D2-4D0B-92CD-E05AC5F3DA4E", "9439E16D-C1D4-4CE6-A493-0E83C079705F"}
tell application "Calendar"
repeat with evtRecord in eventReferenceList
set evt to event of evtRecord
set cal to calendar of evtRecord
set summ to summary of evt
-- Create events in the other calendars
set copied to false
repeat with i from 1 to length of targetCalendarIDs
set targetcal to (first calendar where uid = item i of targetCalendarIDs)
if uid of targetcal uid of cal then
tell targetcal
display notification "Syncing " & summary of evt & " on " & start date of evt & " to calendar " & name of targetcal
make new event with properties {summary:summary of evt, start date:start date of evt, end date:end date of evt, allday event:allday event of evt}
set copied to true
end tell
end if
end repeat
if not copied then display notification "No events where copied during sync"
end repeat
end tell
end main
--display dialog "done!"
-- Get selected event
-- derived from
-- Returns a list of records {event, calendar}
on selectedEvents()
-- get selected event localid from ical plist
set defaultsReply to (do shell script "defaults read SelectedEvents")
set selectedEvts to parseDefaults(defaultsReply)
if selectedEvts = {} then
error "No Calendar Event Selected"
end if
-- convert
set eventReferenceList to {}
repeat with sEvent in selectedEvts
set {eventID, calendarID} to sqlQuery(sEvent)
tell application "Calendar"
set eventReference to event id eventID of calendar id calendarID
set eventRecord to {event:eventReference, calendar:calendar id calendarID}
set end of eventReferenceList to eventRecord
end tell
end repeat
return eventReferenceList
end selectedEvents
-- parse plist SelectedEvents output
on parseDefaults(resultText)
set localUIDs to {}
set {TID, text item delimiters} to {text item delimiters, quote}
set resultItems to text items of resultText
set text item delimiters to TID
repeat with i from 1 to (count resultItems)
if i mod 2 = 0 then set end of localUIDs to resultItems's item i
end repeat
return localUIDs
end parseDefaults
-- Convert localid to applescript uids
on sqlQuery(localUID)
local dateString, localUID
if localUID contains "/" then
set {TID, text item delimiters} to {text item delimiters, "/"}
set {dateString, localUID} to text items of localUID
set text item delimiters to TID
end if
set sqlText to "
SELECT DISTINCT zcalendaritem.zshareduid AS eventID
, znode.zuid as calID
FROM zcalendaritem
JOIN znode
ON znode.z_pk = zcalendaritem.zcalendar
AND zcalendaritem.zlocaluid = '" & localUID & "'
set sqlPath to POSIX path of (path to library folder from user domain) & "Calendars/Calendar Cache"
set {TID, text item delimiters} to {text item delimiters, "|"}
set {eID, cID} to text items of (do shell script "echo " & quoted form of sqlText & " | sqlite3 " & quoted form of sqlPath)
set text item delimiters to TID
return {eID, cID}
end sqlQuery
on error err
display dialog err
end try
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.