Skip to content

Instantly share code, notes, and snippets.

@dehowell
Created December 31, 2012 15:03
Show Gist options
  • Save dehowell/4420448 to your computer and use it in GitHub Desktop.
Save dehowell/4420448 to your computer and use it in GitHub Desktop.
Python script for converting iCal events (using the appscript module) to a CSV file.
import csv
import re
import sys
import appscript
def get_calendar(name):
calendar = appscript.app('iCal').calendars[name]
try:
calendar.get()
return calendar
except appscript.CommandError:
return None
def parse_summary(summary):
m = re.match('(?:(?P<category>\[.+?\]) ?)?(?P<title>[^\[\]]+)?', summary)
if m is not None:
d = m.groupdict()
return d['category'], d['title']
else:
return None, None
writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONNUMERIC, quotechar='"')
writer.writerow(['category', 'title', 'start', 'duration'])
for event in get_calendar('Time Tracking').events.get():
category, title = parse_summary(event.summary.get())
start = event.start_date.get()
end = event.end_date.get()
duration = int((end - start).total_seconds() / 60)
writer.writerow([category, title, start, duration])
@dehowell
Copy link
Author

Events are title following the convention:

[category] title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment