Adding Printers in Leopard

Hello! I am new to Applescript, and I am trying to get write code that adds a new IP printer for my school.

So far, this is what I could come up with by looking at examples on the internet, along with Prefab UI Browser. I don’t know if the “set value” lines will work, b/c this code chokes on the 6th line, saying that it can’t get toolbar 1. I think I have the syntax right. Help?

I’ve created screenshots for users to do it manually here: http://www.samsoncreations.com/fullermacprinting/leopard.html but I want to completely automate the process.

tell application "System Events"
	launch
	tell application "AddPrinter" to activate
	delay 1
	tell process "AddPrinter"
		click button "IP" of tool bar 1
	end tell
	
	--Set Protocol
	set value of pop up button 1 of group 2 of group 1 to "Line Printer Daemon - LPD"
	
	--Set IP address
	set value of combo box 2 of group 2 of group 1 to "192.168.70.2"
	delay 1
	
	--Set Queue
	set value of combo box 1 of group 2 of group 1 to "studentprinter"
	delay 1
	
	--Set Name
	set value of text field 1 of group 1 of group 1 to "Fuller Library Printer"
	delay 1
	
	--Set Location
	set value of text field 2 of group 1 of group 1 to "Library Print System"
	delay 1
	
	--Add Printer
	click button 4
	
end tell

Inspiration for this came from: http://bbs.applescript.net/viewtopic.php?id=16234

Similarly, I did have this code:

tell application "System Events"
	launch
	tell application "System Preferences" to activate
	delay 1
	tell process "System Preferences"
		click menu item "Print & Fax" of menu "View" of menu bar item "View" of menu bar 1
	end tell
	delay 1
--Add Printer
	click button 6 of window "Print & Fax"
	
end tell

But it always died on the last line. I was unable to address that window, even though it was clearly called “Print & Fax”. It keep on insisting that it could not “get” it. Then I found that the “Add Printer” was its own application, so now I am able to call it directly.

Hi,

try this (unfortunately the shell command accepts no space character in the name, although they are escaped)


property IPaddress : "192.168.70.2"
property Queue : "studentprinter"
property printername : "Fuller_Library_Printer"
property printerlocation : "Library Print System"

do shell script "lpadmin -p '" & printername & "' -v " & quoted form of ("lpd://" & IPaddress & "/" & Queue) & " -P /usr/share/cups/model/generic.ppd -o printer-is-shared=false  -L " & quoted form of printerlocation & " -E"
tell application "System Preferences"
	activate
	reveal anchor "print" of pane id "com.apple.preference.printfax"
end tell

the GUI scripting way could be like this


activate application "AddPrinter"
tell application "System Events"
	tell process "AddPrinter"
		repeat until exists window 1
			delay 0.5
		end repeat
		tell window 1
			click button "IP" of tool bar 1
			repeat until exists pop up button 1 of group 2 of group 1
				delay 0.5
			end repeat
			tell pop up button 1 of group 2 of group 1
				click
				click menu item "Line Printer Daemon - LPD" of menu 1
			end tell
			
			tell group 1
				--Set IP address
				set value of combo box 2 of group 2 to "192.168.70.2"
				
				--Set Queue
				set value of combo box 1 of group 2 to "studentprinter"
				
				--Set Name
				set value of text field 1 of group 1 to "Fuller Library Printer"
				delay 1
				--Set Location
				set value of text field 2 of group 1 to "Library Print System"
			end tell
			
			tell button 4
				repeat until enabled
					delay 0.5
				end repeat
				click
			end tell
		end tell
	end tell
end tell

Wow, this works beautifully StefanK!

One last question: the newly added printer always becomes the new Default Printer. Is there any way to change it back to whatever is the user’s normal default printer?

The last screenshot on on this page shows what I am talking about (just scroll down):

http://www.samsoncreations.com/fullermacprinting/leopard.html

You can wrap the script with

set Previous_Printer to last word of (do shell script "lpstat -d")
-- main script
do shell script "lpoptions -d" & space & Previous_Printer

StefanK, you have been INCREDIBLY helpful. Thank you so much.

I eventually opted for the shell instead because it runs SO much more quickly, so thank you for that. Also, it does not change the default printer for the user, so it is an improvement from the GUI scripting. Earlier, I had played around the lpadmin command directly in Terminal, but I could never get any printer I added to show up on the Printer Window. Oh well.

I realized that the shell will probably run in Tiger (10.4) and Panther (10.3). Is this true? If this is so, then I won’t need to rewrite script to adapt to those operating systems, which use the Printer Setup Utility, I think.

Also, if you are ok with it, I would like to credit you on the site for your scripts. I would just like to give credit where credit is due.

Respectfully,

Andy

the shell is part of OS X since zero hour.
I’m sure lpadmin works in Tiger, you should prove it in Panther

Feel free to do it :slight_smile: