Hello Everyone;
I have an InDesign CS2 application, written in AppleScript, that allows our group to make thumbnails of images, and then save the completed document as a PDF. The application allows the user to select a destination printer to print the completed PDF. I used to accomplish the print option on our old machines (OS X 10.3.9) by using “do shell script”:
do shell script "lpr -P \"" & theTargetPrinterName & "\" -# " & theNumberOfCopies & " \"" & thePOSIXPathToPDF & "\""
This worked at the time, and we’ve since moved up to iMacs running OS X 10.4.8, and from that time forward, the above line didn’t work anymore. I looked at the “Printer Setup Utility”, ran through the printer names, and the “Printer Names” were valid. As I’ve become painfully aware researching this, the lpr command uses the “Queue Name” for the target printer. The Queue Name is often times different from the Printer Name, depending on the naming convention used by, and the mood of, the system administrator. Apparently before our upgrade, the names were the same. The Queue Name is visible from the info for each printer in the Printer Setup Utility, and the result from parsing a “lpstat -a” could be wildly different from the actual Printer Name.
Now my question:
Is there a way to programmatically obtain the “Queue Name” based on a list of printer names? (From some property of printer Y maybe?)
The idea is something along this line:
tell application "Printer Setup Utility" to set thePrinters to (name of printers)
set thePrinter to printer (choose from thePrinters)
set thePrinterQueue to <Queue Name> of thePrinter
do shell script "lpr -P \"" & thePrinterQueue & "\" -# " & theNumberOfCopies & " \"" & thePOSIXPathToPDF & "\""
Till then, I’ll just go to each machine, kick the printers out and set the Queue Names to the Printer Names. I truly hope someone here can save me the worries of doing something like that.
Thanks in advance,
Len
Model: iMac
AppleScript: 1.10.7
Browser: Firefox 2.0.0.3
Operating System: Mac OS X (10.4)
does
lpstat
look at the Man page
give you info
Hi Mark,
Thanks for responding. Running lpstat without any switches yields nothing; completely blank; and just returns me to a command prompt. lpstat -a has been the closest thing I could come up with so far. I thought I found a solution, from the Printer Setup Utility dictionary, but so far, it just times out when I use it:
tell application "Printer Setup Utility"
print alias (thePDFFile as POSIX file) with properties {copies:pNumberOfCopies, collating:true, target printer:pSelectedPrinter} without print dialog
end tell
Sorry was on my way out and had a 5 second look at lpstat,
but thought posting lpstat -a would be misleading. hence the look at man page.
My friend has just come back from Mexico visiting family. And has brought back very good tequila, so I am in no state to investigate at the mo, but will tomorrow. Thank good for apples spell checker is all i can say.
I think I may be making some progress, FINALLY. lpstat -l -p is the answer. I was trying combinations of switches, such as lpsat -pl, lpstat -lp, and they were failing miserably. lpstat -l -p, at least for me, is the only way I’ve found to get some detailed printer info including the printer name (in this case, Description). I was having some issues using sed against the output, so I resorted to perl -pe. Namely, I couldn’t get sed to remove the tab character from the output. Here’s what I’m thinking of throwing into my get printers function:
set AppleScript's text item delimiters to {return}
set thePrinterNames to text items of (do shell script "lpstat -l -p | grep \"Description: \" | perl -pe \"s;\\t;;g\" | perl -pe \"s;Description: ;;g\"")
set theQueueNames to text items of (do shell script "lpstat -l -p | grep \"^printer \" | perl -pe \"s;^printer ;;g\" | perl -pe \"s; is .*enabled since .*\\$;;g\"")
set AppleScript's text item delimiters to {""}
set theDefaultQueue to (do shell script "lpstat -d | perl -pe \"s;system default destination: ;;g\"")
-----------------------------
set thePrinterInfo to {}
if (thePrinterNames as string) ≠"" then
repeat with i from 1 to (count thePrinterNames)
set theDefaultPrinter to false
if (item i of theQueueNames) = theDefaultQueue then set theDefaultPrinter to true
set thePrinterInfo to thePrinterInfo & {{printerName:item i of thePrinterNames, queueName:item i of theQueueNames, isDefault:theDefaultPrinter}}
end repeat
end if
thePrinterInfo
It’s not terribly pretty, but the bonus is that I don’t have to query the Printer Setup Utility at all. One less app to launch. Let me know what you’re able to come up with.
Where the ( awk -F’ ) is there is a tab following the -F ,that I copied and pasted from a result. putting in my own did not work.
set thePrinterNames to (do shell script "lpstat -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ") as list
set theQueueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'") as list
set theDefaultQueue to (do shell script "lpstat -d | perl -pe \"s;system default destination: ;;g\"") as list
set thePrinterInfo to {}
set theDefaultPrinter to false
if theQueueNames = theDefaultQueue then set theDefaultPrinter to true
set thePrinterInfo to thePrinterInfo & {{printerName:item 1 of thePrinterNames, queueName:item 1 of theQueueNames, isDefault:theDefaultPrinter}}
thePrinterInfo
Did you mean for the {{printerName:i parts to come out as they do??
try
set thePrinterInfo to thePrinterInfo & {"printer Name: " & item 1 of thePrinterNames, "Queue Name: " & item 1 of theQueueNames, "Is Default: " & theDefaultPrinter}
if not
What I wanted was a list of properties for each printer; the name, queue, and whether or not it was the default printer (in case the user didn’t save a preference). So printerInfo came out like this:
{{printerName:“Adobe PDF 8.0”, queueName:“AdobePDF8”, isDefault:false}, {printerName:“PR01WDET-PRA4113X”, queueName:“PR01WDET_PRA4113X”, isDefault:false}, {printerName:“PRA4152 (Black & White)”, queueName:“PRA4152__Black___White_”, isDefault:false}, {printerName:“PRA4152C (HP Color LaserJet)”, queueName:“PRA4152C”, isDefault:true}}
Now when the user selects a destination printer, a quick loop through printerInfo for a matching printerName now allows us to return a corresponding queueName, so:
repeat with theInfo in printerInfo
if printerName of theInfo = userSelectedPrinterName then
do shell script "lpr -P \"" & queueName of theInfo & "\" -# " & theNumberOfCopies & " \"" & thePOSIXPathToPDF & "\""
exit repeat
end if
end repeat
It’s been working great since noon today. I’ve never used awk to parse strings; I should look into that.
Forgive me for reopening this thread after so many years.
I’m trying to write some AppleScript that will display a list of printers (easy) and return the name of the printer queue that corresponds to the printer that the user selects.
This thread seems to give me a lot of the pieces that I need, but I’ve spent an hour or so trying to fit them together without success.
It’s probably obvious to an expert how to get the queue name by following the procedure in the message dated 2007-03-23, but I can’t figure out how to extract items from the list. I would be grateful for any help.
To answer my own question: This seems to work:
set printerNames to (do shell script "lpstat -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ") -- as list
set queueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'") -- as list
set printerList to (every paragraph of printerNames) as list
set queueList to (every paragraph of queueNames) as list
choose from list printerList with prompt "Choose a printer:"
set thePrinter to item 1 of result
set item_num to my list_position(thePrinter, printerList)
set theQueue to item item_num in queueList
display dialog theQueue
on list_position(this_item, this_list)
repeat with i from 1 to the count of this_list
if item i of this_list is this_item then return i
end repeat
return 0
end list_position
Many thanks to other people whose code I adapted.
I hope it’s OK to reopen this thread, because I’ve found a problem (and a solution) to the code I posted in the previous message.
If one of the installed printers isn’t available, then the queue list doesn’t match the printer list. I figured out this kludge for getting the two lists in line by removing the “reason unknown” line that appears under the name of the unavailable printer, but I can guess that there is a much better way to do this. Here’s my code (not the complete code above, just some added lines to clean up the queue list); I’ll be grateful for any more effective way to accomplish the same thing:
set queueNames to (do shell script "lpstat -a | awk -F' accepting' '{print $1}'")
set queueList to (every paragraph of queueNames) as list
set removeList to {}
repeat with i from 1 to count of queueList
if item i of queueList contains "reason unknown" then
set removeList to removeList & i
end if
end repeat
set newQueue to {}
if removeList is not {} then
repeat with j from 1 to count of queueList
if j is not in removeList then set end of newQueue to item j of queueList
end repeat
end if
set queueList to newQueue
Again, I don’t pretend that this code is any good. All improvements will be welcome.