Advertisement
Guest User

Copy iCal events

a guest
Sep 6th, 2011
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --iCal Event Duplicator.scpt
  2. --Writen by Mike Cramer
  3. --January 10, 2011 ~ 4pm CST
  4. -- Modified by Marco Dondero: added recurrent events and checks to avoid copying an already existing event
  5. -- September 4, 2011
  6. --
  7. --Assume GPL style license.
  8.  
  9.  
  10. to getRecurrenceTermination(startDate, recurrenceString)
  11.     set olddel to AppleScript's text item delimiters
  12.     set AppleScript's text item delimiters to ";"
  13.     set tItems to text items of recurrenceString
  14.     set AppleScript's text item delimiters to "="
  15.     set d to 0
  16.     set untl to missing value
  17.     repeat with anItem in tItems
  18.        
  19.         set parts to text items of anItem
  20.         set sec to word 3 of anItem
  21.        
  22.         if (offset of "FREQ=" in anItem) > 0 then
  23.             if (offset of "WEEKLY" in anItem) > 0 then
  24.                 set d to 7
  25.             else if (offset of "DAILY" in anItem) > 0 then
  26.                 set d to 1
  27.             else if (offset of "MONTHLY" in anItem) > 0 then
  28.                 set d to 31
  29.             end if
  30.         else if (offset of "INTERVAL=" in anItem) > 0 then
  31.             set d to d * sec
  32.         else if (offset of "COUNT=" in anItem) > 0 then
  33.             set d to d * sec
  34.         else if (offset of "UNTIL=" in anItem) > 0 then
  35.             set untl to current date
  36.             set untl's year to text 1 thru 4 of sec
  37.             set untl's month to text 5 thru 6 of sec
  38.             set untl's day to text 7 thru 8 of sec
  39.             set untl's hours to text 10 thru 11 of sec
  40.             set untl's minutes to text 12 thru 13 of sec
  41.             set untl's seconds to text 13 thru 14 of sec
  42.         end if
  43.     end repeat
  44.     set AppleScript's text item delimiters to olddel
  45.    
  46.     if untl is missing value then
  47.         if d is not 0 then
  48.             set finalDate to startDate + (d * days)
  49.         else
  50.             set finalDate to startDate + (1000 * days)
  51.         end if
  52.     else
  53.         set finalDate to untl
  54.     end if
  55.     return finalDate
  56. end getRecurrenceTermination
  57.  
  58. tell application "iCal"
  59.    
  60.     set TheCalendars to name of calendars
  61.    
  62.     set theSourceCalendar to ""
  63.     set theDestinationCalendar to ""
  64.    
  65.     choose from list TheCalendars with title "Please select a source calendar" without empty selection allowed
  66.     set theSourceCalendar to result as string
  67.    
  68.     if theSourceCalendar is "" then
  69.         --do nothing
  70.     else
  71.        
  72.         set theOtherCals to {}
  73.         repeat with anItem in TheCalendars
  74.             if (anItem as string) is not (theSourceCalendar as string) then set theOtherCals to theOtherCals & anItem
  75.         end repeat
  76.        
  77.         choose from list theOtherCals with title "Please select a destination calendar" without empty selection allowed
  78.         set theDestinationCalendar to result as string
  79.        
  80.        
  81.         if theDestinationCalendar is "" then
  82.             --do nothing
  83.         else
  84.            
  85.             display dialog "Copy calendar events from " & theSourceCalendar & " to " & theDestinationCalendar & "?" buttons {"OK", "Cancel"} default button 2
  86.             if the button returned of the result is "OK" then
  87.                 set TheEvents to events of calendar theSourceCalendar
  88.                 set otherEvents to events of calendar theDestinationCalendar
  89.                 repeat with anEvent in TheEvents
  90.                     set curDate to current date
  91.                     set isNew to 1
  92.                     set startDate to start date of anEvent
  93.                     set endDate to end date of anEvent
  94.                     set eventStatus to status of anEvent
  95.                     set recuInfo to recurrence of anEvent
  96.                     set auid to uid of anEvent
  97.                     if recuInfo is not missing value then
  98.                         set ed to my getRecurrenceTermination(startDate, recuInfo)
  99.                     end if
  100.                     if endDate ≥ curDate and eventStatus is not none then
  101.                         --check that is not already existing using uid of events
  102.                         repeat with oEvent in otherEvents
  103.                             set ouid to uid of oEvent
  104.                             if ouid is equal to auid then
  105.                                 set isNew to 0
  106.                                 exit repeat
  107.                             end if
  108.                         end repeat
  109.                         if isNew is not 0 then
  110.                             duplicate anEvent to end of calendar theDestinationCalendar
  111.                         end if
  112.                     else
  113.                         log "Event discarded: old"
  114.                     end if
  115.                    
  116.                 end repeat
  117.             else
  118.                 --do nothing
  119.             end if
  120.         end if
  121.     end if
  122. end tell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement