Section 10.6. Printing from the Command Line


10.6. Printing from the Command Line

Since the Mac OS X printing infrastructure is built on top of various Unix tools, it should be no surprise that you can print from the command line. You can print plain-text, PDF, and PostScript files from the command line by using the following command:

     lp filename

You can print the following kinds of files from the command line to any printer:

  • Plain text

  • PDF

  • PostScript

Because Mac OS X supports all these technologies in its print system, you don't have to worry about sending PostScript to a non-PostScript printereven if the printer is an old dot matrix printer. For example, to print out the contents of a PostScript file named requirements.ps, you would use the command shown in Example 10-5.

Example 10-5. Printing a text file to a printer
 $ lp requirements.ps request id is HP_LaserJet_5000-6 (1 file(s))

CUPS tells you that the job has been submitted and provides you with an identifier for the job. The identifier is composed of two parts: the name of the printer that the job will be printed onin this case, HP_LaserJet_5000and a sequence numberin this case, the number 6 indicates that this is the sixth job printed from this machine since CUPS was launched.

When called by itself, lp prints to the default printer on the system. To print to another printer, you have to specify its queue to the lp command. But how do you know the names of the printers attached to your computer? Use the lpstat command (refer back to Example 10-1 to see this in action).

Armed with this information, you can use the lp command with the -d option, as shown in Example 10-6. The -d option tells lp which printer to select as its destination.

Example 10-6. Printing a PDF file to a specific printer
 $ lp -d HP_LaserJet_1012 test.pdf request id is HP_LaserJet_1012-7 (1 file(s))

10.6.1. Working with PostScript

Panther has added several features for working with PostScript files. For example, you can open PostScript files (which usually have the .ps extension) using Preview. When you do, Preview converts the PostScript to PDF and then displays the result in its window.

There's also a command-line tool, pstopdf, which you can use to convert a PostScript file to PDF. For example, to convert a PostScript file named stats.ps, you would use the following command:

     $ pstopdf stats.ps

This creates a stats.pdf file. To specify a different output filename, use the -o option:

     $ pstopdf stats.ps -o results.pdf

Another helpful utility is enscript, which converts plain-text files to PostScript. This command uses the following syntax:

     enscript outputfile filename

where the outputfile argument is the name of the file to save PostScript data to and the filename argument is the plain-text file to convert. For example, to convert your .bash_profile file to PostScript, you could use the following command:

     $ enscript -p profile.ps .bash_profile

If you call enscript without the -p option, it prints the converted PostScript output directly to the default printer on the system. A nice feature of the enscript command's -p switch is that you can send its output to STDIN by specifying a hyphen (-) as the outputfile. Team this up with pstopdf's -i switch, which instructs pstopdf to take its input from STDIN, and you can go from a plain-text file to a PDF with a quick stop in PostScript on the way. For example, to make a PDF version of your .bash_history file, enter this command:

     $ enscript -p - .bash_history | pstopdf -i -o History.pdf

10.6.2. Command-Line Printing to AppleTalk-Based Printers

For the most part, printing to an AppleTalk printer from the command line is as easy as printing to any other printer, as long as you have set up the printer using the Printer Setup Utility so it has a print queue on your system. If you don't have the AppleTalk printer set up on your system, the first thing you should do is either set it up or find a shared print queue for that printer on another system. (Life is much simpler when CUPS takes care of things.)

If you don't, or can't, set up the AppleTalk printer using the Printer Setup Utility, then you'll need to use the atprint command. Unlike lp, the atprint command doesn't work with files. Instead, it takes its input from the output of other commands and sends it either to a printer that you specify or to the default AppleTalk printer (which is not the same as the default printer for the system set in the Printer Setup Utility). You also have to be careful to send the right kind of data to the printer. In the case of most AppleTalk printers, this means that you have to send PostScript data. Example 10-7 shows how to send a PostScript file to a printer using atprint.

Example 10-7. Sending a PostScript file to an AppleTalk printer
 $ cat manuscript.ps | atprint Looking for PET:LaserWriter@*. Trying to connect to PET:LaserWriter@*. atprint: printing on PET:LaserWriter@*.

If the data you want to send isn't in PostScript format, you'll need to convert it first (if possible) using enscript. Example 10-8 shows how to print the contents of a plain-text file to an AppleTalk printer named PET.

Example 10-8. Printing a plain-text file to an AppleTalk printer
 $ cat .bash_profile | enscript -p - | atprint PET Looking for PET. Trying to connect to PET:LaserWriter@*. [ 1 pages * 1 copy ] left in - atprint: printing on PET:LaserWriter@*.

The at_cho_prn command is used to set the default printer for use by atprint. You'll need to execute this command as root, as shown in Example 10-9.

Example 10-9. Setting the default atprint printer
 $ sudo at_cho_prn Password: Zone:*???????????^@????????????`?????????` ? 1: ff01.04.9dtPET:LaserWriter ITEM number (0 to make no selection)?1 Default printer is:PET:LaserWriter@* status: idle

All in all, while the atprint command is handy in a pinch, the right thing to do is to set up the printer in the Printer Setup Utility and leverage the power of CUPS and the rest of the Mac OS X printing system to your advantage.




Running Mac OS X Tiger
Running Mac OS X Tiger: A No-Compromise Power Users Guide to the Mac (Animal Guide)
ISBN: 0596009135
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net