Getting Events/Tasks From the OS X Calendar Store in a Custom Format like XML, CSV or LaTeX

Posted on October 13, 2010
Filed under Mac, Programming, Scripts

Over the past couple of years I’ve gotten a few emails from users of icalBuddy who would like to somehow automate the task of generating CSV, XML/HTML or LaTeX output from the items in their calendar. Unfortunately icalBuddy isn’t set up to provide any kind of arbitrary output format (and I didn’t want to re-architect it to do that) but a while ago I finally had the time and motivation to figure out a small solution for this: a Python helper class for writing scripts that produce whatever type of output your heart desires.

This class is called CalStoreHelper and it provides you with two methods: one for getting events and the other for getting tasks. An example script shows you concretely how to generate different kinds of output. Below is a small excerpt that shows how to generate CSV output:

import datetime
from CalStoreHelper import *
from CalendarStore import *


c = CalStoreHelper()

# List of calendars to query (use None for all calendars)
calendar_names = ['Home','Work']

# Start and end dates to get events between
start_date = datetime.datetime.now()
delta = datetime.timedelta(days=10)
end_date = start_date + delta

def example_CSV():
    # Example: get events + CSV formatting
    events = c.getEvents(calendar_names, start_date, end_date)
    from UnicodeCSV import UnicodeWriter
    csv_writer = UnicodeWriter(open('/dev/stdout', 'wb'))
    for event in events:
        csv_writer.writerow([
            event.calendar().title(),
            event.title(),
            event.location(),
            event.startDate().descriptionWithCalendarFormat_timeZone_locale_(
                                None, None, None),
            event.endDate().descriptionWithCalendarFormat_timeZone_locale_(
                                None, None, None),
            event.notes()
            ])

You can get the class by cloning the version control repository with Mercurial or by downloading an archive from the repository browser page (the bz2, zip and gz links at the top). Happy hacking.

Comments

No Responses to “Getting Events/Tasks From the OS X Calendar Store in a Custom Format like XML, CSV or LaTeX”

Show/hide comments & reply form: