Add Windows Printer - With Advanced Settings

Hi,
Apologies in advance if this has been posted in the incorrect place - I’m new. :slight_smile:

I am new to Applescript and have been trying to write a script that will automate adding a windows printer. I’ve found the only way to get to the detailed level that I require is to use a UI script (I could be wrong about that).

The script I have partially written/borrowed from the web mostly works but there are a couple of problems with it.

  1. The first time you run it, it fails, but if you run it again with the addprinter dialogue box already open it works. :o/ I can’t work out the reason for the initial fail.
  1. If the Advance button has not already been added to the addprinter window (using customize toolbar) then the script fails. I’m having great difficulty trying to work out how I can script the addition of the button if it doesn’t exist.

Heres what I have got so far…


tell application "AddPrinter" to activate
tell application "System Events" to tell process "AddPrinter"
	tell toolbar 1
		tell menu 1
			tell menu item "Customize Toolbar." --This bit to add the Advanced button but doesn't appear to work.
			end tell
		end tell
	end tell
	click button "Advanced" of toolbar 1 of window 1
	tell group 1 of window 1
		tell group 2
			repeat until pop up button 1 exists
				-- searching for printers may well take several seconds
			end repeat
			click pop up button 1
			click menu item "Windows printer via spoolss" of menu 1 of pop up button 1
			delay 2
			set value of text field 1 to "smb://PRINTSERVER"
			delay 1
		end tell
		tell group 1
			set value of text field 1 to "PRINTER"
			click pop up button 1
			click menu item "Select Software." of menu 1 of pop up button 1
			delay 2
			set uiScript to keystroke "PRINTDRIVER"
			key code 48
			key code 125
			delay 2
			key code 36
		end tell
	end tell
	delay 2
	tell window 1
		click button "Add"
	end tell
	delay 3
	key code 36
	tell group 1 of group 1 of sheet 1 of window 1
		--UI elements of pop up button 1
		tell pop up button 1
			click
			tell menu 1
				-- key presses to select menu items for the prefrecnces as I couldn't get it to work any other way.
				click menu item "5 paper trays installed"
				key code 48
				key code 48
				key code 125
				key code 125
				key code 125
				key code 36
				key code 36
			end tell
		end tell
	end tell
end tell
--tell application "AddPrinter" to quit	

Really hope that all makes sense. Any help/advice would be much appreciated.

Thanks
Goldi

Model: iMac
Browser: Firefox 32.0
Operating System: Mac OS X (10.8)

For anyone that is having the same issue I have managed to fumble my way to a solution that works! Possibly not the tidiest code in the world but it works for me. :D:D:D:D


tell application "AddPrinter" to activate
tell application "System Events" to tell process "AddPrinter"
	tell toolbar 1 of window 1
		perform action "AXShowMenu"
		delay 0.5
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 125
		key code 36
	end tell
	tell toolbar 1 of sheet 1 of window 1
		tell image 2 --"Advanced toolbar item"
			perform action "AXShowMenu"
			delay 1
			key code 125
			key code 36
			key code 36
		end tell
	end tell
	click button "Advanced" of toolbar 1 of window 1
	tell group 1 of window 1
		tell group 2
			repeat until pop up button 1 exists
				-- searching for printers may well take several seconds
			end repeat
			click pop up button 1
			click menu item "Windows printer via spoolss" of menu 1 of pop up button 1
			delay 2
				tell text field 1
				set value of attribute "AXFocused" to true
				end tell
			set value of text field 1 to "smb://PRINTSERVER"
			delay 1
		end tell
		tell group 1
			set value of text field 1 to "QUEUENAME"
			click pop up button 1
			click menu item "Select Software." of menu 1 of pop up button 1
			delay 2
			set uiScript to keystroke "PRINTDRIVER"
			key code 48
			key code 125
			delay 2
			key code 36
		end tell
	end tell
	delay 2
	tell window 1
		click button "Add"
	end tell
	delay 3
	key code 36
	tell group 1 of group 1 of sheet 1 of window 1
		tell pop up button 1
			click
			tell menu 1
				-- key presses to select menu items for the preferences as I couldn't get it to work any other way.
				click menu item "5 paper trays installed"
				key code 48
				key code 48
				key code 125
				key code 125
				key code 125
				key code 36
				key code 36
			end tell
		end tell
	end tell
end tell
--tell application "AddPrinter" to quit