Using the Printer Setup Utility in OS X is a very easy and simple way to create printers on a Mac. But occasionally you might have need to create printers at the command line. For example, you might want to create printers on remote machines that only have shell access. Or you might want to add the ability to create printers to a login script or a script that runs after a machine is re-imaged.
Well, the command to do this is
lpadmin is used mainly to set up network printers. It’s the command line utility for CUPS, the underlying printing architecture of OS X. As usual you can type
man lpadmin to get all the gory details. In this post I’m going to cover how to create a printer and how to delete one using Terminal and
Creating a new printer
The syntax to create a new printer is:
/usr/sbin/lpadmin -p "name of printer" -E -v lpd://"printer IP or DNS"/"queue name" -P "path to PPD file" -D "description"
The name of the printer is whatever you want the user to see, such as “color laser printer”. Just remember if your name has spaces in it you’ll either need to escape the spaces or quote the name. Also, the name cannot contain any non-printable characters (ex. % $ &). The description field, however, can. If you use the description field that is the name that appears in Printer Setup Utility. If you don’t then the name given in the
- p flag will be the name.
All of the standard PPD files on a Mac are kept in /Library/Printers/PPD/Contents/Resources/en.lprog/. The actual PPD file will be contained in a .gz file. You just need to point to that file.
So, for our example we’ll set up a printer with the following attributes:
Type: HP Color Laser 4700
Print server: print.example.com
print queue: color_laser
description: Color Laser (Front Office)
So our command in the Terminal would be this:
usr/sbin/lpadmin -p Color_Laser -E -v lpd://print.example.com/color_laser -P /Library/Printers/PPDs/Contents/Resources/en.lproj/HP\ Color\ LaserJet\ 4700.gz -D "Color Laser (Front Office)"
Note that I quoted the “Color Laser (Front Office)” part to get around spaces in the name. That’s the name that appears in Printer Setup Utility.
Deleting a Printer
Deleting a printer is much easier. All you need is the name.
The syntax to delete a printer is:
/usr/sbin/lpadmin -x "name of printer"
If I wanted to delete the printer I just created all I would need to do is this:
/usr/sbin/lpadmin -x "Color Laser"
Note: The name you’re deleting is the name you gave it in the
- p flag, not the description. So putting “Color Laser (Front Office)” in this would not work.
If you’ve forgotten what names you gave the printers open up a web browser on your machine and enter “http://127.0.0.1:631/printers”. That will take you to the configuration page for CUPS, which will list the printers by name.
Creating printers in AppleScript
You can wrap all of these commands up in an AppleScript and send it to users so they can install printers with just a click. Just wrap the commands in a
do shell script command.
The one gotcha is because you are using quotes in the Terminal command AND you have to quote to actual command in AppleScript you have to escape the internal quotes by putting a “\” before each quote. You also have to escape and “escapes” you had in the original command.
To compare, here is the original command:
/usr/sbin/lpadmin -p "Color_Laser" -E -v lpd://print.example.com/color_laser -P /Library/Printers/PPDs/Contents/Resources/en.lproj/HP\ Color\ LaserJet\ 4700.gz -D "Color Laser (Front Office)"
And here is the command with the “do shell script” command in AppleScript:
do shell script "/usr/sbin/lpadmin -p Color_Laser -E -v lpd://print.example.com/color_laser -P /Library/Printers/PPDs/Contents/Resources/en.lproj/HP\\ Color\\ LaserJet\\ 4700.gz -D \"Color Laser (Front Office)\""
admin :: Apr.20.2007 ::
Applescript, Management, Scripts, shell scripts, Terminal ::
2 Comments »