Checkboxes reverting state on click or perform action AXPress

I am working on a personal project to automate Internet Sharing to Wi-Fi clients via VPN (or Ethernet – depending on the use case). In addition, I intend to automate the happy path configuration which involves enabling and/or disabling ports in the Internet Sharing sub-pane. In all use cases, the “Wi-Fi” port should be enabled and all other ports disabled.

Here is the problem –

In any given state of this set of checkboxes, I can correctly identify the checkbox associate with the “Wi-Fi” port and those that are not. I can correctly determine whether or not the checkbox is enabled or disabled and send either a “click” or “perform action AXPress” event to the UI element. I can observe that the script is sending the events, but each checkbox immediately reverts to its original state. Furthermore, there are (random) occasions where the state changes and remains changed for that run of the script – then remains fixed on subsequent runs.

Here is the script –

tell application "System Preferences"
	activate
end tell

tell application "System Events"
	tell process "System Preferences"
		click menu item "Sharing" of menu "View" of menu bar 1
		tell window "Sharing"
			set serviceTable to table 1 of scroll area 1 of group 1
			
			-- Select Internet Sharing sub-pane
			tell (first row of serviceTable whose value of static text 1 is "Internet Sharing")
				set selected to true
			end tell
			
			delay 1
			
			if (value of checkbox 1 of (first row of serviceTable whose value of static text 1 is "Internet Sharing") is 0) then -- Internet Sharing disabled
				set portsTable to table 1 of scroll area 2 of group 1
				
				-- Enable Wi-Fi
				tell (first row of portsTable whose value of text field 1 is "Wi-Fi")
					tell checkbox 1
						if (value is equal to 0) then
							perform action "AXPress"
						end if
					end tell
				end tell
				
				-- Disable all other ports
				tell (every row of portsTable whose value of text field 1 is not "Wi-Fi")
					tell checkbox 1
						if (value is equal to 1) then
							perform action "AXPress"
						end if
					end tell
				end tell
				return true
			end if
		end tell -- window "Sharing"
	end tell -- process "System Preferences"
end tell -- application "System Events"

Any thoughts? I am befuddled.

AppleScript: 2.2.1
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Hi.

It seems to work for me if I set both the row’s ‘selected’ and ‘focused’ properties to ‘true’ before clicking the checkbox:

tell (first row of portsTable whose value of text field 1 is "Wi-Fi")
	set selected to true
	set focused to true
	tell checkbox 1
		if (value is equal to 0) then
			perform action "AXPress"
		end if
	end tell
end tell

The section to uncheck the other boxes probably has to be done in the same way, using a repeat.