Pages

AppleScript Calender Automation

Here's a problem that sounds straight forward, but isn't really.

The issue:

My work calendar is in Office365, and colleagues can put in meeting requests. The problem is that forget to add any time between meetings to get between them.  So I wanted to automatically add 15 minutes either side.

In theory is should be fairly straightforward.  But...

1) A can't see anyway to trigger a script 'On New Event' so it needs to run as a regular batch
2) Applescript Calendar/iCal automation is ridiculously slow.  I'm not sure if this is because of the Office365 connection, but any attempt to search for items times out.

So in the end, this strategy could run without timing out (that doesn't mean it runs quickly... go make a cup tea while it runs)

1) Get all events (anything else timed out)
2) Copy the events for the next month to a new list by iterating through.
3) Run a bubble sort on those events (I need to look at the previous event to see if was already a 'pre-event gap' event.

This still doesn't deal with repeated events.  I think code to deal with those would take forever to 

Here's the code (I really need to get a proper code formatting plugin!)



tell application "Calendar"
 tell calendar "Calendar"
  set today to (current date)
  
  set endDate to today + (31 * days)
  
  log "started"
  
  
  set theEvents to every event
  
  log "got events"
  
  set moreEvents to {}
  
  repeat with c in theEvents
   
   set sd to start date of c
   set ed to end date of c
   
   
   if (sd  today and ed < endDate) then
    
    copy c to the end of moreEvents
   end if
  end repeat
  
  log "done filter"
  
  
  
  set theEvents to my sortEvents(moreEvents)
  
  log "sorted events"
  
  repeat with i from 1 to (count theEvents)
   
   set c to item (i) of theEvents
   
   set doEvent to true
   
   if (i > 1) then
    
    set prev to item (i - 1) of theEvents
    
    set ps to summary of prev
    
    if (ps is "Pre-event gap") then
     set doEvent to false
    end if
    
    
   end if
   
   
   set sd to start date of c
   set ed to end date of c
   
   set s to summary of c
   
   if (doEvent is true and s is not "Pre-event gap" and s is not "Post-event gap") then
    
    
    log s
    
    if not (allday event of c) then
     
     set beforeStart to sd - (25 * minutes)
     set beforeEnd to sd - (1 * minutes)
     
     make new event with properties {start date:beforeStart, end date:beforeEnd, summary:"Pre-event gap"}
     
     set afterStart to ed + (1 * minutes)
     set afterEnd to ed + (15 * minutes)
     
     make new event with properties {start date:afterStart, end date:afterEnd, summary:"Post-event gap"}
     
    end if
    
   end if
  end repeat
 end tell
end tell

on sortEvents(theList)
 
 tell application "Calendar"
  
  set sorted to false -- assume they're out of order
  repeat until sorted = true
   set sorted to true
   repeat with i from 1 to (count theList) - 1
    -- check two items
    if (start date of item i of theList > start date of item (i + 1) of theList) then
     -- they're out of order, so set the flag
     set sorted to false
     -- swap the items (via a temp object
     copy item (i + 1) of theList to tmpEvent
     set item (i + 1) of theList to item i of theList
     set item i of theList to tmpEvent
    end if
   end repeat
  end repeat
  
 end tell
 -- and return the sorted list
 return theList
end sortEvents

No comments:

Post a Comment