Simulate desktop printers using CUPS

Nov 10, '02 06:36:18PM

Contributed by: googoo

As a college professor, I have many files that I keep in PDF format (handouts, advising documents, publications, etc.) When I need to print one of these files, I find it tedious to go through the standard procedure (open it with an application, choose Print, etc.). In the old days of Desktop Printers, I could drop the file icon on the printer icon, and, at least for some file types, the procedure was simplified. This option was not available in OS X (to my knowledge). But as of 10.2, it's CUPS to the rescue! For more info on CUPS, which is standard on Mac OS X 10.2, see several previous hints or point your browser to http://localhost:631/. To print without using the originating application, open the Terminal and navigate to the file's directory and enter:

 % lp filename
Replace 'filename' with the name of the file to print. The file prints without opening an application. This works for text, PDF, PostScript, and many graphics files. You can also type
 % lp[space]
where [space] represents hitting the space bar. Then, drag the file icon to the terminal, and hit Return. The file will print regardless of your directory.

I wrote the following (very simple) AppleScript application to go one step farther and print a file(s) dropped onto its icon. Type the following into the Script Editor, or download the text file and paste it into the editor:

on open fileList
display dialog ¬
"Print file(s) using the default CUPS printer?" ¬
buttons {"OK", "Options", "Cancel"} ¬
default button "OK"
set doWhat to button returned of result
if (doWhat = "OK") then
repeat with theItem in fileList
set thePath to POSIX path of theItem
do shell script "/usr/bin/lp " & thePath
end repeat
else if (doWhat = "Options") then
display dialog "Enter options for the lp command:" ¬
default answer ""
set theOpts to the text returned of result
repeat with theItem in fileList
set thePath to POSIX path of theItem
do shell script "/usr/bin/lp " & theOpts ¬
& " " & thePath
end repeat
end if
end open
Note that there is a space after /usr/bin/lp in both of the "do shell script" lines.

Save the file as an Application and put it in a convenient location. If you drop an appropriate file on the icon, it will print. If you add the application to your Dock, you can just drop the file on the Dock Icon, and it will print.

Read the UNIX man page for lp (enter "man lp" no quotes) to find out about the options that you can use.

Comments (9)


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