Switch on "Share this printer on the network"?

I am trying to write an AppleScript that will turn on the “Share this printer on the network” option for a specific printer if that option is off. I think I can this via GUI scripting, but I don’t know how to select the specific printer so that I can use GUI scripting to switch on that option.

I would be very grateful if someone could point the way to selecting a specific printer in the Print & Fax dialog. Thank you for any help.

To answer my own question - though only in part - the following (based on scripts posted by others in this forum) works correctly when I type into the script the exact name of a printer instead of using the variable name “printerName” in the subroutine:


property on_ : 1
property off_ : 0

tell application "Printer Setup Utility"
	set printerList to name of every printer
end tell
choose from list printerList with prompt "Choose a printer to share:"
set thePrinter to (item 1 of result)

switchPrinterSharing(on_, on_, thePrinter)

on switchPrinterSharing(val, state, printerName)
	tell application "System Preferences"
		activate
		reveal anchor "Main" of pane id "com.apple.preferences.sharing"
	end tell
	tell application "System Events"
		tell process "System Preferences"
			tell table 1 of scroll area 1 of group 1 of window 1
				tell (1st row whose value of static text 1 is "Printer Sharing")
					if value of checkbox 1 is not val then
						click checkbox 1
					end if
				end tell
			end tell
			tell table 1 of scroll area 3 of group 1 of window 1
				tell (1st row whose value of text field 1 is printerName) -- works with specific name, not variable
					if value of checkbox 1 is not state then
						click checkbox 1
					end if
				end tell
			end tell
		end tell
	end tell
end switchPrinterSharing

When I use the variable printerName in the subroutine, then the script acts unpredictably - it seems to toggle printer sharing on and off each time I use it, and when it toggles it on, it turns on sharing for the last printer that was selected in the dialog.

I’m obviously doing something wrong that is simple - but I’m afraid to say I don’t see it. I would be grateful to anyone who can tell me what I am doing wrong.

I finally figured out that this needed the line “set selected to true” immediately after the line that tells the row with the printer name in it. Here’s a revised version that seems to work correctly:

set printerNames to (do shell script "lpstat  -l -p | grep -i Description: |awk -F'Description: ' '{print $2}' ")
set printerList to (every paragraph of printerNames) as list
choose from list printerList with prompt "Choose a printer:"
set thePrinter to item 1 of result

set printerName to thePrinter as Unicode text

tell application "System Preferences"
	activate
	reveal anchor "Main" of pane id "com.apple.preferences.sharing"
end tell

tell application "System Events"
	tell process "System Preferences"
		tell table 1 of scroll area 1 of group 1 of window 1 		
			tell (1st row whose value of static text 1 is "Printer Sharing")
				if value of checkbox 1 is 0 then
					click checkbox 1
				end if
			end tell
		end tell
		tell table 1 of scroll area 3 of group 1 of window 1
			tell (1st row whose value of text field 1 is thePrinter)
				set selected to true
				if value of checkbox 1 is 0 then
					click checkbox 1
				end if
			end tell
		end tell
	end tell
end tell