Fantastical and multiple events

For seven or eight years when my sons were much younger, I coached various park district and YMCA sports teams. I had no special talent for the sports, but I was very good at communicating with the parents. At the beginning of each season, I’d send out an email with everything they needed to know: practice schedule, game schedule, phone numbers, email addresses, and most important of all, the date, time, and location of the team picture. I took special pride in providing the game schedule as both a list—which could be printed out or copied into a paper calendar—and as an attached .ics file for importing into iCal or Google Calendar or Outlook.

This was a bit ahead of the curve. While everyone back then had cell phones with a calendar feature, very few took advantage of that feature because it was hard to use. And while most of the working parents kept their business calendar on a computer, personal calendars still tended to be on paper. There were no iPhones or Android phones at the time, and BlackBerrys and Treos were usually reserved for “serious” information like meeting dates and golf outings.

Still, there were a few families who appreciated the .ics files, and it was easy for me to make them once I had the schedule entered into iCal. After my retirement from coaching came the age of smartphones for everyone, and I assumed that my kids’ sports schedules—now in the hands of schools with IT departments—would all be available in electronic form.

So far, that hasn’t happened, and I’m resigned to the expectation that my younger son will be out of high school before the fine IT professionals for the local school district realize that an .ics download is more useful than yet another half-assed HTML calendar in which every event has to be clicked on to get the necessary details.

School swimming calendar

So I’ve tried to come up with ways to make entering several similar calendar entries as painless as possible. Back when I was using TextMate, I had a system in which I entered one event per line with pipe characters (|) separating the description, date, time, duration, and location fields. These would then be converted into an .ics file and imported into iCal. That worked fairly well, but errors were sometimes hard to catch because the pipe separators made the lines noisy and difficult to read.

Enter Fantastical and its natural language parsing. Even better, enter Fantastical’s small but powerful AppleScript dictionary that allows you to write programs that use its NLP. I recently a very short script for parsing lines like this

Swimming vs. Hinsdale Central at 5pm on 1/16 at Hinsdale Central /home
Swimming vs. Lake Park at 10am on 1/17 at NCHS /home
Swimming Sandburg Invitational at 10am on 1/24 at Sandburg /home

and entering them into my personal calendar (which is called home). The script is called caladd:

python:
 1:  #!/usr/bin/python
 2:  
 3:  from applescript import asrun, asquote
 4:  from sys import stdin
 5:  
 6:  cmd = 'tell application "Fantastical" to parse sentence %s add immediately true'
 7:  for line in stdin:
 8:    fantastical = asquote(line.rstrip())
 9:    asrun(cmd % fantastical)

It uses the simple applescript library I wrote a couple of years ago that lets me run AppleScript commands from within Python.

As you can see, there’s not much to the script. It reads each line from standard input, wraps it in quotes, and feeds it to Fantastical’s parse sentence command. It can be run from the command line or from within many text editors.

Is writing a file with a bunch of lines like this really easier than just typing them directly into Fantastical’s entry field? That depends on the power of your text editor and how good you are at exploiting that power. The lines have a lot of repeated text, and if you’re good at using things like copy-and-paste, search-and-replace, column editing, and multiple cursors, you’ll probably find that entering 15–20 lines in a file is distinctly faster.

If you’re lucky, though, your kids will go to a school with a more enlightened IT staff. Then you’ll just download a file and pop it right into your calendar. And I will hate you for it.