Shell-command alternatives to Printer Setup Utility?

I’m writing in the hope that someone has solved some of these problems already.

For various reasons, I want to remove all references to the Printer Setup Utility in my scripts, and replace them with shell commands. I see that I can use lpoptions and lpstat for most purposes, and I wonder if anyone has already figured out how to do the following:

  1. I distribute some scripts that check whether a printer is available, before trying to print. How can I use the output of lpstat to check the number of “enabled” printers? (I want to count printers that are listed as “enabled”, not the ones that are listed as “disabled”.)

  2. How can I get the name of the default printer through a shell command?

  3. I use this code to print a file and report whether a file is still being printed. How is it possible to do this with shell commands instead of “Printer Setup Utility”:

tell application "Printer Setup Utility"
	open thisItem -- "thisItem" is the file being printed
		tell application "System Events"
			repeat while (busy status of thisItem) 
				delay 1
			end repeat
		end tell
end tell

Many thanks for any help with any or all of these questions.

To answer my own question to 2 - how to get the name of the default printer, it’s:

set Def_Printer to word 4 of (do shell script "lpstat -d")

That’s from another post on this forum, which I should have seen earlier.

And to answer my own question 1 (how to tell if no printer exists)

	set ptrCount to (do shell script "lpstat -p | grep -c 'enabled'")
	display dialog "Printers available: " & ptrCount
on error errMsg
	if errMsg is "0" then
		display dialog "No printer available."
		display dialog errMsg
	end if
end try

That leaves question 3 - how to print a file and know when it is finished printing, through a shell command.

There is a shell command named pr but as far as I know, it’s restricted to text files.

KOENIG Yvan (VALLAURIS, France) lundi 2 septembre 2013 20:17:54

The one that seems to work is lpr but I’m still testing. I wanted to know when the file was no longer in use so that I could delete it - and the lpr command has an optional -r switch that deletes the file after printing, so this accomplishes everything I wanted. But I still want to test it further before being certain of it. (I haven’t tested it with non-PostScript printers.)