How to print with ghostscript in silent mode

Extracted from ghostscript documentation, MS Windows Printers

If no Windows printer name is specified in -sOutputFile, Ghostscript prompts for a Windows printer using the standard Print Setup dialog box. ...

If a Windows printer name is specified in -sOutputFile using the format "%printer%printer_name", for instance

gs ... -sOutputFile="%printer%Apple LaserWriter II NT" 

then Ghostscript attempts to open the Windows printer without prompting (except, of course, if the printer is connected to FILE:)

Depending of the how the final process should work, maybe you will also need the -dBATCH and -dNOPAUSE switches

notes: %printer% is a literal to Ghostscript but the syntax will collide with cmd parser that sees %printer% as a variable read operation.

Inside batch files the percent signs can be escaped by doubling them (%%printer%%) but in command line mode there is not any way to escape the percent sign if it is inside a quoted string (as shown in the documentation, they are needed to handle spaces in arguments).

In command line mode there are two alternatives:

  • Ensure that the printer environment variable does not exist (if it does not exist, then the read operation is not executed and the literal %printer% is keeped in the final executed command).
  • Define some environment variable so when the read operation is executed the resulting command will be correct.

Just some command line examples:

rem ensure the variable does not exist
set "printer="
gs ... -sOutputFile="%printer%Apple LaserWriter II NT"

rem store the full literal
set p=%^printer%
gs ... -sOutputFile="%p%Apple LaserWriter II NT" 

rem store the percent sign
set "p=%"
gs ... -sOutputFile="%p%printer%p%Apple LaserWriter II NT"