Print postscript file to printer

Hello all,

I’m able to print a postscript file that resides on my desktop by dragging it to the printer queue window that is launched from the “Print & Fax” system preference. I am trying to find a way to do this with Applescript. All my attempts have failed.

Here is my latest attempt:


set PSFILE_PATH to (path to desktop) & "test.ps" as string

tell application "Printer Setup Utility"
	set PRINT_CONFIG to {target printer:"hp color LaserJet 5550"}
	print PSFILE_PATH with properties PRINT_CONFIG
end tell

Any suggestions would be greatly appreciated,
CarbonQuark

I use lpr accessed via do shell script and it works fine. It uses POSIX paths instead of AS paths and if you’re in Tiger you’ll need to fix the printer name to exchange all spaces and dashes with underscores, e.g. HP Color Laser becomes HP_Color_Laser. I don’t know what other characters, if any, need to be replaced. With that in mind, if you have the (corrected) printer name in a variable, printerName, and the POSIX path for the PS file to output in a variable psFile, you could use the following:

do shell script "lpr -P " & quoted form of printerName & " -l " & quoted form of psFile

Joseph,

Thanks for your reply, I really appreciate it!

I tried the following and am getting an Applescript error.


set psFile to POSIX path of (path to desktop) & "test.ps"
set printerName to "hp_color_LaserJet_5550"
do shell script "lpr -P " & quoted form of printerName & " -l " & quoted form of psFile

Applescript error: “lpr: error - unable to print file: client-error-not-found”

I don’t have much experience with the shell script command so I am not sure how to trouble shoot this. I played around with different syntax and each attempt yields the same error. The file “test.ps” is on my desktop. I am using Tiger (10.4.5)

Thanks,
CarbonQuark

Hey Joseph,

I found an old thread that you had posted. It looks like you had the same problem. I ran this script that Adam posted:


set P to paragraphs of (do shell script "lpstat -p")
set Ptrs to {}
repeat with aP in P
   set end of Ptrs to word 2 of contents of aP
end repeat
Ptrs

My printer is “hp_color_LaserJet_5550_” not “hp_color_LaserJet_5550”

“_” at the end seems odd, but it worked.

Thanks for you help and your original post.

It’s working like a charm!

CarbonQuark

One more question about the shell script command. If I wanted to read the postscript file in and make an edit using Applescript and then push it to the printer how would the shell script read?

For example: (doesn’t work; for illustrative purposes only)


set f to open for access POSIX path of (path to desktop) & "test.ps" as POSIX file with write permission
set POSTSCRIPT_DATA to read f
close access f

-- Manipulate the data "POSTSCRIPT_DATA" code would go here... (I want to change a name in the postscript data using TIDs)

set printerName to "hp_color_LaserJet_5550_"
do shell script "lpr -P " & quoted form of printerName & " -l " & POSTSCRIPT_DATA

Thanks,
CarbonQuark

You can manipulate PS ASCII text with AppleScript, no need for do shell script. You would use the read command to read the file into a variable and then manipulate as needed. Depending on the size of the PS, it might not always work though. What size limit causes the error, I don’t know.

To get the text into a variable with the read command and a POSIX path, use something like:

set POSTSCRIPT_DATA to read POSIX file psFile

If you’re looking to do basic search and replace, I’ve got a simple handler for that (below). If it’s something more than that, explain and I’ll see if I’ve got something for it.

on searchAndReplace(searchText, matchStr, replaceStr)
	set resultText to {}
	set {tid, my text item delimiters} to {my text item delimiters, matchStr}
	try
		set resultText to every text item of searchText
		set my text item delimiters to replaceStr
		set resultText to resultText as text
		set my text item delimiters to tid
	on error
		set my text item delimiters to tid
	end try
	return resultText
end searchAndReplace

After you’re done messing with the text you can write it back to the file, replacing the text already there.

set writeTarget to (open for access POSIX file psFile with write permission)
try
	set eof of writeTarget to 0
	write POSTSCRIPT_DATA to writeTarget starting at eof
	close access writeTarget
on error
	close access writeTarget
end try

I’ve done very little messing around with PostScript so I don’t really know what caveats might apply. And I don’t really know if the resulting PS file will still work, though I can’t imagine why not. I figure you might want to watch out for using AS text for certain characters and use the ASCII character equivalent instead, e.g. ASCII 13 instead of ‘return’. And I don’t know, and seriously doubt, if this could work with binary PS.

Joseph,

Thanks for all the help!

That will work just fine! I have a .ps file that I will be using as a template for the data. I will use you code to write it out as a different name and then simply print using your shell command. That should work no problem. Your TID subroutine will do the trick. Postscript is just a text file.

Maybe there is a way to cut out the step of saving it out to a file as a different name (thus not overwriting the template) and simply pushing the postscript data from the variable POSTSCRIPT_DATA directly to the printer. Not a big deal though your solution will work.

Thanks again!
CarbonQuark