Better way to find a printer name in applescript

Hello,

I am trying to get a file to print using an applescript:

tell application "Printer Setup Utility"
	
	set the current printer to printer "HP Photosmart C4700"
	open (alias "Macintosh HD:Users:barbarapress:Documents:report.txt")
	
end tell

I am getting an error that says:

error “Printer Setup Utility got an error: Can’t set printer "HP Photosmart C4700" to printer "HP Photosmart C4700".” number -10006 from printer “HP Photosmart C4700”

I have tried to put the printer name in a few ways, but same error.
Is there a way to use applescript to get the name of the printer to put into a script?

thanks!
babs

Hi,

do you want to get or set the printer?

This is an universal handler to print a file via lpr


on print_lpr(thePath, thePrinter)
	try
		-- do shell script pr_options
		do shell script "lpr " & "-P " & thePrinter & space & (quoted form of POSIX path of thePath)
		return true
	on error
		return false
	end try
end print_lpr

thePath is a HFS path or alias to the file
thePrinter is the name of the printer. The name must match the syntax which is displayed in Terminal with lpstat -a

Hi Stefan…

That script did not give me a result? ;-(

However, I played with the get/ set thing you mentioned and I came up with this script and it worked, despite having a missing value as the result?

tell application "Printer Setup Utility"
	
	get the current printer
	open (alias "Macintosh HD:Users:barbarapress:Documents:report.txt")
	
end tell

weird huh?
babs :wink:

of course, you must call the handler with

print_lpr("Macintosh HD:Users:barbarapress:Documents:report.txt", "HPPhotosmartC4700")

please check the correct spelling of the printer name

Hi Stefen,

I actually did do that originally, I swear… :wink:
but I got the following error, so I thought I was doing it wrong:

error “«script» doesn’t understand the print_lpr message.” number -1708 from «script»

that’s when I tried it without that info, and got nothing.

thanks!!'babs

is the handler call line within any tell block?

ok-I put the script the script inside a tell application “Finder” block and got this error.

error “Finder got an error: Can’t continue print_lpr.” number -1708

babs

If you use handlers in tell-blocks (except tell me to doSomething()), you’ll have to refer to the handler as with my doSomething() or doSomething() of me or tell me to doSomething().

tell application "Finder"
	my doSomething()
	doSomething() of me
	tell me to doSomething()
	
	doSomething() --> errors
end tell

on doSomething()
	display dialog "Hello World!"
end doSomething

Notice that the script itself show the dialog and not the Finder. To let the Finder display a dialog, you should insert the tell-block in the handler itself.

tell application "Finder"
	my doSomething()
	doSomething() of me
	tell me to doSomething()
	
	doSomething() --> errors
end tell

on doSomething()
	tell application "Finder" to display dialog "Hello World!"
end doSomething

Hope it helps,
ief2

hi!
Sorry for the late reply…had to go teach :wink:

Actually, I did get a little lost here, but it’s OK, not an emergency. I’ll move on to the next part of the book…
Maybe all these handlers and tell statements will make more sense as I keep going. Those topics are coming up shortly :wink:
thanks!!
babs