I am very new to postscript and ghostscript How can I pass argument into ps file? I have powershell script that make some calculation and store result in variable $par1 after it I execute something like this
gswin64c.exe -o "result.pdf" -sDEVICE=pdfwrite pdfProc.ps -f "input.pdf"
I want use $par1 value into pdfProc.ps , but I cant find the right way except of write all code from pdfProc.ps into -o parameter
For Ghostscript only, arguments on the command line are preserved in the PostScript environment. This is not usual behaviour for PostScript interpreters so if you use this feature you are limiting your program to working with Ghostscript.
Any switch on the command line which is not otherwise recognised by the device is stored in (IIRC) systemdict. There are two kinds of switches; -s and -d, -s introduces string variables, -d introduces names or numbers.
So to take a concrete example, if I wanted to define a quantity of apples I might do:
gswin64c -dNumApples=4 -sDEVICE=pdfwrite -o out.pdf pdfProc.ps input.pdf
Your PostScript program (pdfProc.ps) could then use the named variable /NumApples, which would have a value of 4 (NB You should generally use known
or where
to check the existence of a variable, to avoid errors if it is not defined).
So a simple program like
%!PS
/NumApples where {
/NumApples get
(Number of apples is ) print ==
}{
(NumApples not defined\n) print
} ifselse
would print the result 4 for the command line above.
BTW I don't undertand what you mean by "except of write all code from pdfProc.ps into -o parameter". The -o switch just specifies the output file name, and implies -dBATCH
-dNOPAUSE
, it doesn't process PostScript or anything like that.