views:

683

answers:

2

Hi, is there any objective C API or object that can give me access to the iCal and its events? I need to read the calendar events for a given date and optionally set a new event.

The code is either plain C or objective C (in the GUI version of the program). I'm using xcode on a mac os 10.6.

Sample code is greatly appreciated.

+2  A: 

The Calendar Store framework provides this functionality.

Jim Correia
Thanks for the info. Do you have a snippet I can use to get me started? Or can you point me to one?Te examples on the Developer Connection are not very clear. Thanks
Uri
What are you stuck on? Calendar Store is very straightforward and friendly if you know the basics of Cocoa already. Start by looking at the documentation for -[CalCalendarStore eventsWithPredicate:]
Mike Abdullah
Mike, thanks. All I need is a sample snippet that actually works. I tried compiling those little pieces of code that Apple posted but I can't get it to compile. I just don't know what imports (or includes) I need to add. So, a simple snippet will solve a lot of problems here.thanks again
Uri
A: 

You might find it easier to use iCal's Apple Events scripting interface. If you don't like the idea of using AppleScript directly, there is Apple's Scripting Bridge or, even better, use Appscript for Objective-C, Ruby, or Python. Here's one way to access and modify the events for a given day using py-appscript:

>>> from appscript import *
>>> from datetime import datetime
>>> start_time = datetime(2009,10,26)
>>> end_time = datetime(2009,10,26,23,59,59)
>>> calendar = app('iCal').calendars['Home']
>>> for event in calendar.events[(its.start_date >= start_time).AND(its.start_date <= end_time)]():
...   event_properties = event.properties()
...   print event_properties[k.start_date], event_properties[k.summary]
... 
2009-10-26 18:40:00 <event 1>
2009-10-26 23:38:00 <event 2>
>>> event.summary.set(to='something else')
Ned Deily
thank you but even tho i like Python and other scripts a lot I can't use that this time. Either C or objective C
Uri
It should be simple to translate that code to the Objective C version of Appscript.
Ned Deily
the reason I asked in the 1st place is because I don't even know what objects or API's I need to be able to talk to the calendar store. So, translating that would be a bit difficult if I don't know what functions I need.
Uri
I'm not sure I understand correctly but, if you want to understand the iCal scripting data model, the standard way to do that - as with any OS X scriptable app - is to introspect the model using the AppleScript Editor (in /Applications/Utilites) Open Dictionary command. But in this case use the tools provided with Appscript (http://appscript.sourceforge.net/tools.html): ASDictionary translates the iCal's data model dictionary into Objective C-appscript syntax and can also automatically generate all the Objective C glue code to access that data model.In any case, good luck.
Ned Deily
Using Apple Events has the *big* disadvantage of needing iCal to be launched when accessing it. The Calendar Store is designed exactly to avoid doing this.
Mike Abdullah
It's true iCal has to be running when using Apple Events. That may be a disadvantage depending on your application. All in all, using the Calendar Store framework is a more robust solution but, for many applications, using Apple Events is perfectly adequate, plus the approach generalizes to most other OS X scriptable applications - even if there isn't a "standalone" API alternative.
Ned Deily
Thanks all, but PLEASE i don't want a script. I clearly wrote that I need to code this in C or objective C. I don't understand why this isn't clear.
Uri
and no, I don't want to try this. and no, I don't want to translate this. Thanks for the answer but it's not what i am looking for.
Uri