• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

John August

  • Arlo Finch
  • Scriptnotes
  • Library
  • Store
  • About

Removing duplicate iCal entries

March 19, 2005 Geek Alert

geek alert This is hugely off-topic, so feel free to skip to the next article, which will likely have something to do with screenwriting and/or filmmaking.

My assistant Chad and I use Apple’s iCal to keep track of appointments. It’s nowhere near as sophisticated as Exchange or a real professional calendar system, but for the most part, it works. He maintains “John’s Work” calendar, and I maintain “John’s Personal” calendar. We both use the built-in publish-and-subscribe feature, so we see the same things on each computer.

After doing this for several years, however, some problems have arisen — mostly stemming from syncing with various Palm devices. Calendar events get duplicated, often six or seven times. Multiply that by several years, and the files get huge, and slow: my Work.ics file ballooned to 1.7 megabytes.

After searching the internet for a program that would fix this, I finally had to write my own. In the interest of paying-it-forward to the next guy with the same problem, here’s what I wrote. Continue on only if you’re truly geeky, or desperate.

UPDATE: Changes with OS 10.4 (Tiger), and specifically iCal 2.0, means that the script as written won’t work anymore. Sorry. But the underlying concept still holds. With an hour and a little ambition, it should be possible to eliminate duplicates in the same way. Just be sure to always work on a backup of the calendar file.

If you open up an iCal file in a text editor, you’ll see that it’s actually very simple. Each entry starts with “BEGIN:VEVENT” and ends with “END:VEVENT”. In-between, you see the start and end times (written as 20041210T130000, which means 2004/12/10 at 1:00 p.m.), a summary of the event, and a UID, which is supposed to keep each entry unique. Unfortunately, events sometimes get imported without the UID’s, which means you can end up with seventeen appointments for picking up the dry cleaning last March.

The best (i.e. simplest) solution I could come up with was to simply sort the file based on start times, and eliminate duplicates. This is a little unforgiving; if you happened to have “Coffee with Jen” and “Conference call w/ Paul Brown” listed at exactly the same date and time, one would get overwritten. But that was a longshot I could live with.

Here’s the Python script I wrote. I like Python because it fits my brain, but you could easily translate it to Perl, Ruby or almost anything. (I’d be tempted to do it in AppleScript, but I find it slow at handling big text files.)

!/usr/bin/python

This script removes duplicate entries from an iCal file,

by building a dictionary keyed from the DTSTART value.

Note: if two different entries have the identical start

time and date, one will be overwritten.

Obviously, work on a backup copy of your iCal file.

import sys

replace next line with path to calendar

the_file = open('/Users/john/Library/Calendars/Work.ics')
the_text = the_file.readlines()

the_dict = {} temp = "" id = ""

for line in the_text: if 'BEGIN:VEVENT' in line: temp = "" temp = line elif 'END:VEVENT' in line: temp = temp + line.rstrip()
the_dict[id] = temp ### note: overwrites if multiple elif 'DTSTART' in line: temp = temp + line id = line ### use 'DTSTART' as key id value else: temp = temp + line

vcalendar headers; replace if yours are different

print 'BEGIN:VCALENDAR' print 'VERSION:2.0' print 'X-WR-CALNAME:Revised Work' print 'PRODID:-//Apple Computer\, Inc//iCal 1.5//EN' print 'X-WR-RELCALID:DC928866-9705-11D9-9E58-000393D00CCE-CALP' print 'X-WR-TIMEZONE:US/Pacific' print 'CALSCALE:GREGORIAN'

use dictionary values; discard keys

for line in the_dict.values(): print line

stardard vcalendar footer

print 'END:VCALENDAR'

You notice that the script reads from a file, but doesn’t write back out. That’s because I’m using BBEdit, and it’s easier just to generate the text file in that.

Related Posts

  1. Celtx screenwriting application shows promise
  2. When Final Draft won’t open under OS X
  3. Rewriting, but where’s the script?

Primary Sidebar

Newsletter

A weekly-ish roundup of stuff we've found interesting delivered right to your inbox.

Read Past Issues

Explore

Projects

  • Arlo Finch (27)
  • Big Fish (87)
  • Charlie (39)
  • Charlie's Angels (16)
  • Chosen (2)
  • Corpse Bride (9)
  • Dead Projects (18)
  • Frankenweenie (10)
  • Go (30)
  • Karateka (4)
  • Monsterpocalypse (3)
  • One Hit Kill (6)
  • Ops (6)
  • Preacher (2)
  • Prince of Persia (13)
  • Shazam (6)
  • Snake People (6)
  • Tarzan (5)
  • The Nines (118)
  • The Remnants (12)
  • The Variant (22)

Apps

  • Bronson (13)
  • FDX Reader (11)
  • Fountain (32)
  • Highland (71)
  • Less IMDb (4)
  • Weekend Read (33)

Recommended Reading

  • First Person (83)
  • Geek Alert (145)
  • WGA (121)
  • Workspace (19)

Screenwriting Q&A

  • Adaptation (66)
  • Directors (90)
  • Education (48)
  • Film Industry (480)
  • Formatting (128)
  • Genres (90)
  • Glossary (6)
  • Pitches (29)
  • Producers (59)
  • Psych 101 (117)
  • Rights and Copyright (95)
  • So-Called Experts (46)
  • Story and Plot (170)
  • Television (161)
  • Treatments (21)
  • Words on the page (236)
  • Writing Process (177)

More screenwriting Q&A at screenwriting.io

© 2021 John August — All Rights Reserved.