Automate printing with specific print settings using Hazel & shell script

Sharing this because I was surprised to find it was such an obscure thing to accomplish.

This will allow you to print to a specific printer on your Mac, using the printer driver’s print settings you specify. This would be similar to using a “Print Preset”, but print settings and print presets are not available to things like AppleScript or Automator.

To do this, you need to use the Terminal and shell scripting. And I ultimately employ this using a Hazel action to automate it.

I piecemealed all of this together from various forums online, which is why I am collating it in one place here.

Step 1: Get Available Print Options For Your Printer

First, you need the printer’s Device Name which is in the System Preferences > Printers & Scanners section, in the General tab of the dialog sheet brought up by the “Options & Supplies” button:

In Terminal, enter:

lpoptions -p Printer_Device_Name

This will output a bunch of printer-specific settings you have available to use to set up your custom print settings.

See this page on the CUPS printer utility for more detail.

In short, in your shell script you want to use -o Option=Value for each setting you want to configure. Anything you do not specify will use the printer’s default settings, which seem to be indicated by the asterisk in the output of the Terminal after running the above command.

Step 2: Build The Shell Script

The generic structure of the shell script will be:

lp -d printer -o Option1=Value -o Option2=Value filename

The -d option specifies the printer, each -o option sets the options you want to customize.

Here is a sample script I am using:

lp -d Canon_LBP622C -o MediaType=PlainPaper3 -o CNTonerDensityAdjustment=True -o CNTonerDensity=11 -o CNCTonerDensity=11 -o CNMTonerDensity=11 -o CNYTonerDensity=11 filename.pdf

Step 3: Use the script with Hazel

For using with Hazel, and using the embedded shell script action, replace the filename with "$1" (include the quotes).

So the final script to be used in an embedded Hazel action would be:

lp -d Canon_LBP622C -o MediaType=PlainPaper3 -o CNTonerDensityAdjustment=True -o CNTonerDensity=11 -o CNCTonerDensity=11 -o CNMTonerDensity=11 -o CNYTonerDensity=11 "$1"

This would be used with a watched folder where you can drop in files, and matching files get processed by Hazel with this script.

For my setup, I wanted to make sure I was using black & white printing for packing slips, but also be able to print color documents with different paper weights and toner density settings for my artwork prints.

And the ultimate goal was to be able to initiate printing from the iPad Pro, which lacks the ability to specify printer driver print settings, or access print presets using AirPrint printers.

Using Shortcuts, I copy or save the desired file into the watched folder, and depending on how the file matches the rule, it prints to the same printer using whatever settings are specified in the shell script embedded in each Hazel action.

I have one Hazel action for the packing slips print settings, and another for the files that match the art print settings, with the embedded shell script set up with the relevant options.

3 Likes

Very good! Well done.

Thank you! Too much work went into figuring this out not to share.

I have been shell scripting for a very long dang time, and lp & lpr are some of the more opaque programs that I’ve tried to do anything with. The syntax seems like it’s straight out of the 1980s.

Yeah I am a novice when it comes to shell scripting, only just started educating myself about a year ago (along with Regular Expressions).

But this entire setup in the post was duct-taped together from those wiser than me. I do not claim any mastery over lp or lpr :)