views:

925

answers:

3

I'm trying to write an AppleScript to query iCal and find all the events I've got for a given date, in any calendar.

I started by writing a simple script that does something simple with every event in a given calendar:

tell application "iCal"
  tell calendar "Reuniones"
 set the_events to every event
 repeat with an_event in the_events
  -- do something with every event
  set value to summary of an_event
 end repeat
   end tell
end tell

However, this simple script is taken a lot of time to execute (a few seconds), even if I'm not doing anything complex inside the loop. I'm afraid that the real script will really take a lot of time to execute.

I'm not very familiar with Applescript, and thus I imagine I'm doing something silly that has severe performance implications.

Can anybody explain me why this takes that much to execute? Can anybody suggest something to improve my code? I'm now going to start checking the date of the event, with a condition in the loop. I suspect there must be a way to search for events with a date (like the Automator action does), but I haven't been able to find a "native" way to do so ....

EDIT: I'm using Mac OS X Tiger (10.4). It is possible that newer versions of iCal have improved the library of operations available.

A: 

My initial intent was to select only the events for a given date, but apparently there aren't methods in iCal to access only the events for a specific day.

Thus, it is always necessary to go over all the events registered in every calendar. Even when interested in the events of a single calendar, say 'Today's Meetings", it is necessary to go through the entire set of events.

The best alternatives I've found around in the web don't use Apple Script, but instead they process the 'ics' files where the info is actually stored.

For reference, those files are located in '~/Library/ApplicationSupport/iCal'.

nozebacle
+2  A: 

I've been grappling with this today and found that you can filter by date (at least on Snow Leopard). So

tell application "iCal"
    set out to ""
    set todaysDate to current date
    set time of todaysDate to 0
    repeat with c in (every calendar)
        set theEvents to (every event of c whose start date ≥ todaysDate)
        repeat with current_event in theEvents
            set out to out & summary of current_event & "\n"
        end repeat
    end repeat
    return out
end tell

will return the summary of all future events, and very quickly, compared to iterating through all events.

Duncan McGregor
Great! Thanks a lot! I had totally lost faith on this question :)
nozebacle
This doesn't show repeating events, though. Is there any way around that?
Dov
I have ended up giving up on the AppleScript API and falling back on iCalBuddy. You'll need to parse its output, but its querying is at least sane!
Duncan McGregor
+1  A: 

It isn't AppleScript, but the best of the bunch of other ways to do this seems to be iCalBuddy, which uses the public Cocoa APIs rather than parsing the calendar file directly and handles repeating events sensibly.

icalBuddy -nc -eed -iep title,datetime eventsToday+1

Duncan McGregor