Created
December 17, 2018 11:14
-
-
Save sbliven/32622b2b5d79524e8d96dd50c07d8777 to your computer and use it in GitHub Desktop.
Copies events between calendars in Apple Calendar.app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 (spencer.bliven@gmail.com) | |
-- | |
-- Event selection code from http://www.johneday.com/1086/reference-selected-calendar-events-applescript | |
-- | |
-- | |
-- Known Limitations: | |
-- | |
-- Running the script multiple times produces duplicate events. | |
------------------------------- | |
-- MAIN CODE | |
------------------------------- | |
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 http://www.johneday.com/1086/reference-selected-calendar-events-applescript | |
-- | |
-- Returns a list of records {event, calendar} | |
------------------------------ | |
on selectedEvents() | |
-- get selected event localid from ical plist | |
set defaultsReply to (do shell script "defaults read com.apple.ical 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 | |
try | |
main() | |
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