Get iCal event info via Applescript for repeating events

Nov 30, '09 07:30:01AM

Contributed by: Andrew J Freyer

Anyone who has ever tried to retrieve the current agenda in iCal via AppleScript realizes how impossible it is. The problem is that iCal's AppleScript keywords seem keyed for adding information to events, not extracting information from those events.

Anyway, the main problem for me was extracting a current event title for use in an AppleScript. Retrieving the title information for a single event was simple. The following code is an example I found with a quick Google search:

tell application "iCal"
  set d to the current date
  repeat with c in every calendar
    tell c
      if exists event 1 then
        set e to (get every event where ((its start date ≤ d) and (its end date ≥ d)))
        if (e is not {}) then return first item of e
      else
        return missing value
      end if
    end tell
  end repeat
end tell
However, because of a bug in iCal, getting current-day information from a repeating event is impossible (I think) from AppleScript itself. My solution is to use the iCalBuddy shell script of GeekTool fame. With that script installed, here's the solution:
set theNextOrCurrentEvent to (do shell script "/usr/local/bin/icalbuddy -ic \"CalendarName\" -eep \"*\" -nc -b \"\" -n -li 1 eventsToday")
This single line will return the summary (title) of the next or current iCal Event in calendar CalendarName, regardless of whether the event is repeating or not.

Comments (1)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20091115201138792