Skip to content

Instantly share code, notes, and snippets.

@dreness
Created April 3, 2020 10:30
Show Gist options
  • Select an option

  • Save dreness/947a96c81c3b9b3bd1f8711d3791e88d to your computer and use it in GitHub Desktop.

Select an option

Save dreness/947a96c81c3b9b3bd1f8711d3791e88d to your computer and use it in GitHub Desktop.
Create an anonymized copy of selected events from a primary calendar into a secondary calendar
set mySourceCal to "cloudcal"
set myTargetCal to "anothercal"
set windowEnd to (current date) + (4 * weeks)
set windowStart to (current date) - (1 * weeks)
tell application "Calendar"
repeat with sourceEvent in (events of calendar mySourceCal where (its start date ≥ windowStart) and (its start date ≤ windowEnd))
set sourceProperties to properties of sourceEvent
set needsUpdate to false
set previouslyCopied to false
set deleteEvent to missing value
set hashString to "printf '%s%s%s%s' " & "'" & (start date of sourceEvent) & "'" & space & "'" & (end date of sourceEvent) & "'" & space & "'" & (allday event of sourceEvent) & "'" & space & "'" & (recurrence of sourceEvent) & "'"
set shellOut to do shell script hashString & "| shasum -a 256"
set sourceEtag to first item of splitText(shellOut, space) of me
set upToDateDesc to sourceEtag & ":" & uid of sourceEvent
tell calendar myTargetCal
set targetEventList to every event
repeat with copiedEvent in targetEventList
set copiedDesc to description of copiedEvent
-- did we copy this event previously?
-- use 'offset' as a substring test
-- offset is zero if the substring (source event UID) is not present
-- in the test string (description of copied event)
set myOffset to (offset of (uid of sourceEvent as text) in copiedDesc)
-- yes, we have copied it previously
if myOffset ≠ 0 then
log uid of sourceEvent & "was previously copied"
set previouslyCopied to true
-- is the source event different from the copy we made last time?
if upToDateDesc ≠ copiedDesc then
log uid of sourceEvent & "has changed"
-- has changed
set deleteEvent to copiedEvent
set needsUpdate to true
exit repeat
else
-- has not changed
log uid of sourceEvent & "has not changed"
set needsUpdate to false
exit repeat
end if
else
log uid of sourceEvent & " is not related to " & uid of copiedEvent
end if
end repeat
if needsUpdate = true or previouslyCopied = false then
if deleteEvent is not missing value then
log "deleting stale copy of " & deleteEvent
delete deleteEvent
end if
set e to make new event with properties {summary:"", start date:start date of sourceEvent, end date:end date of sourceEvent, allday event:allday event of sourceEvent, excluded dates:excluded dates of sourceEvent, description:upToDateDesc}
-- Not sure how to add a property with a 'missing value' value, so only add recurrence if it is present on the source event
if recurrence of sourceEvent is not missing value then
log "Adding recurrence to " & uid of sourceEvent
set recurrence of e to recurrence of sourceEvent
end if
end if
end tell
end repeat
end tell
on splitText(theText, theDelimiter)
set AppleScript's text item delimiters to theDelimiter
set theTextItems to every text item of theText
set AppleScript's text item delimiters to ""
return theTextItems
end splitText
-- ≠
(*
AppleScript docs:
https://developer.apple.com/library/archive/documentation/AppleScript/Conceptual/AppleScriptLangGuide/introduction/ASLR_intro.html
Calendar event properties:
description (text) : The events notes.
start date (date) : The event start date.
end date (date) : The event end date.
allday event (boolean) : True if the event is an all-day event
recurrence (text) : The iCalendar (RFC 2445) string describing the event recurrence, if defined
sequence (integer, r/o) : The event version.
stamp date (date) : The event modification date.
excluded dates (list of date) : The exception dates.
status (cancelled/‌confirmed/‌none/‌tentative) : The event status.
summary (text) : This is the event summary.
location (text) : This is the event location.
uid (text, r/o) : An unique event key.
url (text) : The URL associated to the event.
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment