View upcoming events on multiple Unix calendars

May 15, '06 07:30:00AM

Contributed by: n1mie

I've been leeching off MacOSXHints for years; this is such a great site. So I think it's about time I share some of my own work. This hint was written because I wasn't entirely happy with the output of previous hints, so I kept working at it until I came up with this solution. I wanted a way to view upcoming entries on the various Unix calendar pages -- both the stock calendars, as well as one of my own. Here's what I eventually came up with:

#!/bin/sh
calendar -f /usr/share/calendar/calendar.computer -l 0 > ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.usholiday -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.birthday -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.christian -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.freebsd -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.history -l 0 >> ~/tmpcal.txt
calendar -f /usr/share/calendar/calendar.music -l 0 >> ~/tmpcal.txt
calendar -f ~/.calendar/calendar -l 14 >> ~/tmpcal.txt

cat ~/tmpcal.txt | sort -M
rm ~/tmpcal.txt

The explanation:

Each line that begins with calendar looks up date information in a different calendar file. The -f switch precedes the full path to a calendar file (most of which are in /usr/share/calendar, but you may also have personal ones elsewhere). The -l switch precedes the number of days to look ahead. When look ahead is set to 0, then only today's date information is returned. The tail end of each calendar line directs the input to a temporary file (the >> on all but the first line amends the output to the file created in the first line). The last calendar line looks in my personal file (in my home directory), which contains personal dates of importance.

The line which begins with cat lists the temporary file, and then directs the output to the sort util. sort then uses the first portion of the line to sort by date (so if your information spans one or more months, it should sort properly -- I've done a limited test on this). The last line removes the temporary file.

[robg adds: I tested this, and it seems to work as described. Remember to make the script executable after saving (via chmod a+x scriptname).]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20060510164821945