Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to print out a full page monthly calendar UNIX
Building upon this hint, I wanted to make a full page (8.5" x 11") monthly calendar that could be printed out. So I wrote the attached Perl script to do just that. It prompts you for the month and year, and outputs a plain text calendar suitable for printing. I found that Courier 10pt filled the page up pretty well when printing it. It can't be piped as it stands because of the prompts, but it could be easily modified to take the month and year as arguments like cal instead of prompts so that it could be piped to open -f or enscript.

Please excuse the semi-sloppy coding, I did this late at night :)

To use it, just copy the code, paste it into your favorite text editor (command line or otherwise), save it as whatever you want (I called it cal.pl), make it executable (chmod +x cal.pl) and then run it (./cal.pl). Then copy the output, paste into your favorite text editor, and print it out. Feel free to make improvements, this was a quick and dirty kind of thing.

[robg adds: This script worked as described...]
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[10,995 views]  

A script to print out a full page monthly calendar | 3 comments | Create New Account
Click here to return to the 'A script to print out a full page monthly calendar' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to print out a full page monthly calendar
Authored by: CarlRJ on Feb 15, '05 03:11:51PM
Okay, just for fun, this is a little bit longer, but a lot more Perl-ish. It validates user input thoroughly (always a good habit) and allows for command line arguments.

#!/usr/bin/perl

use strict;
use warnings;

# ===== Parse command line...

my($month, $year);

if (@ARGV == 2 and $ARGV[0] =~ /^\d/) {
    ($month, $year) = @ARGV;
} elsif (@ARGV == 0) {
    $| = 1;
    print "What month do you want to print: ";  chomp($month = <STDIN>);
    print "What year: ";                        chomp($year  = <STDIN>);
} else {
    die "Usage: $0 [ mm yyyy ]\n";
}
die qq{$0: "$month" isn't a month (1..12)\n}
    unless $month =~ /^\d+$/ and $month > 0 and $month <=   12;
die qq{$0: "$year" isn't a year (1..9999)\n}
    unless $month =~ /^\d+$/ and $year  > 0 and $year  <= 9999;

# ===== Read output from "cal", and reformat to a page-sized calendar...

open(CAL, '-|', 'cal', $month, $year)  or die "$0: can't pipe from cal: $!\n";

while (<CAL>) {
    s/$/                    /  if $. > 1;		# Pad partial weeks
    next  if /^\s*$/;					# Drop pesky blank lines

    if ($. == 1) {					# Month name and year
	s/^\s+//;
	print "\n", " " x ((78 - length) / 2), $_, "\n";

    } elsif (/^(..) (..) (..) (..) (..) (..) (..)/) {	# Day names or numbers
	my @days = ($1, $2, $3, $4, $5, $6, $7);

	if (/[A-Z]/) {					# ... Day names
	    print map("|    $_    ", @days), "|\n";
	} elsif (/\d/) {				# ... Day numbers
	    print map("|       $_ ", @days), "|\n";

	    foreach my $n (1..7) { print "|          " x 7, "|\n" }
	}
    }
    print "-" x 78, "\n";
}
close CAL;


[ Reply to This | # ]
A script to print out a full page monthly calendar
Authored by: kmue on Feb 16, '05 05:26:27AM

Very nice! But how to print Monday as the weeks first day?



[ Reply to This | # ]
pcal - full-page calendars in Postscript
Authored by: victory on Feb 16, '05 05:05:00AM
NOTE: This is the method I use to print out full-page calendars. However, I just want to state for the record that I appreciate the contribution of rael9's perl script and the willingness to share it with the rest of us. What follows is NOT an attempt to 'one-up' rael9's method -- it's just what I've been using for several years and I thought others might find it useful.


Basically, I use pcal, an open-source, cmd-line app that generates nice (graphics, fonts, shading, etc.) full-page calendars in Postscript. (pcal is written in C and thus you'll need to have the Apple Dev Tools installed and be familiar with how to compile apps from the Unix cmd line. However, it's a pretty simple app to build. I'm not aware of any pre-compiled OSX/Darwin binaries available on the web)

As mentioned, pcal creates calendars in Postscript. Thus if you happen to have a Postscript-compatible printer hooked up to your Mac (or Ghostscript installed) and want a calendar for July 2005, just type:

pcal 7 2005 | lpr

What if you don't have a Postscript printer? No problem, if you're running 10.3 or later. First create a Postscript file of the calendar page:

pcal 7 2005 > july.ps

then double-click on the july.ps file in the Finder (or use open july.ps) and it will automatically convert the Postscript file to a PDF and open it in the Preview app, from which you can then print the calendar as you would any other document.

Or if you're like me and want to do everything from the command line, try:

pcal 7 2005 | pstopdf -i -o /tmp/cal.pdf ; open -a Print\ Center /tmp/cal.pdf

NOTES:

  • pstopdf is a new command included with 10.3. See the man page for more details.
  • It would be really nice if the above sequence could be done entirely with pipes (without a temporary file), but pstopdf doesn't write to stdout, nor does open read from stdin. And the occasionally helpful trick of using - in place of a filename to indicate stdio doesn't work here. :-( Maybe someone else can figure out a way to do it with named pipes or something...
  • If you're going to give pcal a try, be sure to check out the numerous options it offers. For example, it gives you the ability to include miniature calendars for the previous and next month, phases of the moon, customizable date formats, and more.
  • In the examples shown, I actually have to specify pcal -X 20 , otherwise my printer (a HPLJ 6MP) chops off the edge of the calendar.
  • I can't quite explain why I prefer printing calendars from the cmd-line rather than simply using iCal. But it looks like I'm not the only one...


[ Reply to This | # ]