File folder labels
August 18th, 2005 at 12:14 am by Dr. Drang
Following the same lines as the previous post, I’m posting here a short Perl program I wrote for printing file folder labels. I use Avery 1 by 4 inch mailing labels—or the Maco clones—to label manilla folders and 1/3-cut file tab inserts.
As with address labels, my goal for printing file folder labels is to achieve a uniform look to the labels with as little input from me as possible. My work files are organized by project. As each project has a name and number, I want those printed, in bold, at the top of the label to make project identification easy, with the name flush left and the number flush right. Then the description of the folder contents is given on the next line or two, centered on the label in a normal font. My best HTML approximation of the layout is this:
|
|
||||||||
|
|
||||||||
|
|
||||||||
One way to do this is to structure the input to have the label entries separated by blank lines with the top line of each entry containing the project name and number. But since I often need several labels for the same project—and I really don’t want to be retyping the project name and number for each one—I created a simple “language” that allows me to enter the project info once for a series of labels. These “header” lines start with a #. If a label entry doesn’t start with one of these lines, the previously-defined header is used.
Also, I use the | character to separate the project name and project number. The natural—natural for troff, that is—way to separate these is with a tab character, but I wanted something that was visible. I was certain I’d never have a | in a project name, so that’s what I chose. Seven years later, the choice still seems right. Here’s an example of the input:
#Kernighan Building|4215
Drawings
Contract
Correspondence
Photographs and
videotape
#Ossanna Residence|4332
Report
Correspondence
This will yield six labels shown above.
The program that follows is a recently-rewritten version. The comments made regarding the address label program apply to this program as well.
#!/usr/bin/perl
use Getopt::Std;
# Usage/help message.
$usage = <<USAGE;
Usage: pflabels [options] [filename]
Print file folder labels on Avery 5161 sheets
-r m : start at row m (range: 1..10; default: 1)
-c n : start at column n (range 1..2; default: 1)
-h : print this message
If no filename is given, use STDIN. A label entry is a plain text
series of non-blank lines. Blank lines separate entries.
The first line of an entry is special. If it starts with a #, then it's
considered a header line. Everything in the header line up to the | is
printed flush left in bold and everything after the | is printed flush
right in bold. Subsequent lines are printed centered in normal weight.
If the first line of an entry doesn't start with #, it uses the header
of the previous entry.
USAGE
# Set up geometry constants for Avery 5161.
$topmargin = 0.55;
$poleft = 0.55;
$poright = 4.7;
$lheight = 1;
# get starting point from command line if present
getopts('hr:c:', \%opt);
die $usage if ($opt{h});
$row = int($opt{r}) || 1; # chop off any fractional parts and
$col = int($opt{c}) || 1;
# Bail out if position options are out of bounds
die $usage unless (($row >= 1 and $row <= 10) and
($col >= 1 and $col <= 2));
# Set initial horizontal and vertical positions.
if ($col == 1) {
$po = $poleft;
} else {
$po = $poright;
}
$sp = ($topmargin + ($row - 1)*$lheight);
# Pipe output of this program to groff and the printer (manual feed).
open OUT, "| groff | lpr -Plpm";
# open OUT, "> labels.rf"; # for debugging
select OUT;
# Set up document.
print <<SETUP;
.ps 12
.vs 14
.ll 3.25i
.ta 3.25iR
SETUP
# The troff code for formatting a single entry, with placeholders for
# positioning on the page. The magic numbers embedded in the formatting
# commands make the layout look nice.
$label = <<ENTRY;
.sp |%.2fi
.po %.2fi
.ft HB
%s
.ft H
.ce 3
%s
.ce 0
ENTRY
# Slurp all the input into an array of entries.
$/ = "";
@entries = <>;
$bp = 0; # we don't want to start with a page break
foreach $body (@entries) {
# Parse and transform the header and body.
if ($body =~ /^#/) { # it's a header line
($header, $body) = split(/\n/, $body, 2);
$header = substr($header, 1);
$header =~ s/\|/\t/;
}
$body =~ s/\s+$//;
# Break page if we ran off the end.
if ($bp) {
print "\n.bp\n"; # issue the page break command
$bp = 0; # reset flag
}
# Print the label.
printf $label, $sp, $po, $header, $body;
# Now we set up for the next entry.
if ($col == 1){ # last entry was in the left column
$col = 2; # so the next will be in
$po = $poright; # the right column
} else { # last was in the right column
$col = 1; # so the next will be in
$po = $poleft; # the left column
$row++; # of the next row
if ($row > 10) { # we're at the end of the page
$bp = 1; # page break flag
$row = 1; # new page starts at top row
}
$sp = ($topmargin + ($row - 1)*$lheight);
}
}




