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

Simulate desktop printers using CUPS System
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.
    •    
  • Currently 3.33 / 5
  You rated: 1 / 5 (3 votes cast)
 
[13,550 views]  

Simulate desktop printers using CUPS | 9 comments | Create New Account
Click here to return to the 'Simulate desktop printers using CUPS' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Alternatives
Authored by: eno on Nov 10, '02 07:31:36PM

Could also make droplets for this using ScriptGUI or DropScript, if you\'ve got an aversion to AppleScript...



[ Reply to This | # ]
This does not work for all files
Authored by: kirkmc on Nov 11, '02 03:40:16AM

I had already discovered this technique for printing files, but unfortunately it doesn't work for all types of files. It does work for pdf files and text files, but it does not work for .doc files. Perhaps it only works for files that are supported by the OS - those files that can be opened by Preview, maybe.

It's a shame it doesn't work for others - I have 40 Word files to print one day, and the only way was under Word to print them one at a time.

Kirk



[ Reply to This | # ]
OnMyCommand
Authored by: szabesz on Nov 11, '02 03:49:31AM

There is a freeware ContextualMenu plug-In for running shell scripts, it is called OnMyCommand. I use it for deleting ".DS_Strore" files in nested folders before wrinting a CD-ROM (I hate to write those files onto ISO CDs). By clicking on a file you can define it for the script as a parameter, so you can develop scripts to perform actions on a file or folder! This might me used here too.



[ Reply to This | # ]
problem with filename spaces
Authored by: tomem on Nov 16, '02 05:05:15PM

Great tip. I really miss being able to drop files on desktop printers, too.

One problem is the "POSIX path" does not pick up spaces in filenames properly, i.e. with a backslash in front of them. Therefore this doesn't work for filenames with spaces, for example the screen shot files that the OS X generates, of the form "Picture N.pdf". By the time you've played around fixing the filename, you might as well have opened up Preview and printed...

But it's fine for "well-behaved" unix filenames...



[ Reply to This | # ]
problem with filename spaces
Authored by: googoo on Nov 19, '02 01:11:24PM

I think you can get around the space-in-filename problem by inserting a quote in the UNIX (shell script) command generated by the AppleScript. Do this by changing the first do shell script line to

do shell script "/usr/bin/lp " & "'" & thePath & "'"

Note that there is a single quote inside a pair of double quotes on either side of & thePath &.You would have to simillarly modify the second command. I will test this and see if it works.

-Mark



[ Reply to This | # ]
problem with filename spaces
Authored by: googoo on Nov 19, '02 01:39:33PM

The solution that I proposed above works. The quotes around the file path ensure that spaces are treated as characters rather than as seperators between different file names.

Thanks for pointing out this problem with my script. I have very few file names with spaces and did not notice it.

-Mark

P.S. I am correcting the script that I posted to include this improvement.



[ Reply to This | # ]
Simulate desktop printers using CUPS
Authored by: EricW on Nov 20, '02 07:44:43AM

I can't seem to get the script to work spaces in path or no.
Tried printing the same Appleworks doc from the terminal using lp but got the following "lp: unable to print file: client-error-document-format-not-supported"
Can I clarify too how on earth do you set the default printer to be pdf ?



[ Reply to This | # ]
Simulate desktop printers using CUPS
Authored by: googoo on Nov 20, '02 09:11:09AM

The script ONLY works for document types that the lp command can print directly. This includes text, PDF, PostScript, and most image files (TIFF, PNG, JPEG, etc.). If your file is not one of these types, you must print it from within its creator application because the lp command does cannot interpret the file. Since AppleWorks is not a format that lp "understands," you must print it using the AppleWorks application. When you do so, the application transmits the information to the printer in the appropriate format (often PostScript) for the printer.

-Mark



[ Reply to This | # ]
Simulate desktop printers using CUPS
Authored by: EricW on Nov 21, '02 06:54:03AM

Thanks Mark. I hadn't really understood your original post.
I have the opposite problem to you. Hundreds of files in word and AW that I want to batch print either to postscript for distiller or straight pdf (doable in 9 with Print to PDF desktop printer except AW 6 locks up) so that I can then index them in acrobat for key word searches and burn to cd.
Oh well back to scouring the net.



[ Reply to This | # ]