Quick and Dirty GUI Calendar

Printer-friendly version

A while back, I needed a yearly calendar for my X desktop. Since, at the time, I ran a desktop that did not include an integrated Calendaring facility, I couldn't just click on something. So, I built a GUI perpetual calendar, in a shell script.

The script (which I called Xcal.sh) looked like
#!/bin/bash
WHEN=`date +%Y`
RC=0
while [ $RC -ne 103 ] 
do
  PREV=`expr $WHEN - 1`
  NEXT=`expr $WHEN + 1`

  cal $WHEN | \
  xmessage -buttons $PREV,$NEXT,Quit -file - -title "In the Year $WHEN"

  RC=$?
  case $RC in
  101) WHEN=$PREV
     ;;
  102) WHEN=$NEXT
     ;;
  *) RC=103
     ;;
  esac
done
exit 0

When launched, the script first determined the current year, the "previous year" year, and the "next year" year. Using the current year as a starting point, it generated a calendar using the cal(1) command, with the output piped into the xmessage(1) command. The script gave xmessage three buttons: a button labelled with the "previous year" year, a button labelled with the "next year" year, and a button labelled "Quit". Xmessage displayed the generated calendar on the X server, and awaited a button press to terminate. If the user selects the "previous year" button, xmessage terminates with a 101 returncode. If the user selects the "next year" button, xmessage terminates with a 102 returncode. Any termination value (the "Quit" button, or an X "close" or "kill" termination) is treated as a 103 returncode.

Since the script runs the cal / xmessage pipeline in a while loop that only terminates when a 103 returncode is detected, a "previous year" or "next year" selection causes the calendar to be recalculated. For "previous year", the previously calculated "previous year" is used as the calendar current year, and new "previous year" and "next year" values are calculated before the cal / xmessage pipeline is run. On the other hand, for "next year", the previously calculated "next year" is used as the calendar current year, and again, new "previous year" and "next year" values are calculated before the cal / xmessage pipeline is run.

And the pipeline loops again.

So, with each loop, the user is presented with a calendar for the full year, and a choice to go back one year, forward one year, or quit.




In an exchange with "The Answer Gang" at the Linux Gazette, better minds than mine evolved a much-improved version of the script (which I've called Xcalendar.sh):
#!/bin/bash
# Original idea by Lew Pitcher
# Tweaked by Ben Okopnik, Tue Aug 23 12:30:55 EDT 2005
# Re-tweaked by Lew Pitcher, Tue Aug 23 23:13:47 EDT 2005
# Re-tweaked by J Random Hacker, Wed Aug 24 19:34:21 EDT 2005

echo 'xmessage*font: w-lucidasanstypewriter-10'|xrdb -merge -
year=`date +%Y`

while :
do
        case `cal $year|xmessage -buttons Next,Prev,Quit -file - -title "In the year $year" -default Quit -print`
        in
                Next)   (( year++ )) ;;
                Prev)   (( year-- )) ;;
                Quit)   exit 0       ;;
                *)      exit 1       ;;
        esac
done

Articles: