How to check/uncheck proxies checkbox in System Preferences/Network

Hello, I am just starting with KM but already felt it's tremendous potencial. I am trying to build a macro to check/uncheck proxies settings when I switch from home to work wireless network.

I've tried a series of IF THEN's, with buttons, menu's, etc, and nothing worked.

I'd aprreciate if someone could shed a light in this matter. Thanks

Hi Ricabude,

Please use this AppleScript in KM to select the buttons, simply replace the word "Ask to join new networks" with the exact name of the checkbox you would like to select.

Everytime the script below executes it will automatically tick and then untick if you execute it again.

activate application "System Preferences"
tell application "System Events"
	tell process "System Preferences"
		click checkbox 1 of row 1 of table 1 of scroll area "Select a protocol to configure:" of group 1 of tab group 1 of sheet 1 of window "Network"
	end tell
end tell

However, if you need to get the value before you execute the above script for an IF statement then you can use

activate application "System Preferences"
tell application "System Events"
	tell process "System Preferences"
		get value of checkbox 1 of row 1 of table 1 of scroll area "Select a protocol to configure:" of group 1 of tab group 1 of sheet 1 of window "Network"
	end tell
end tell

If result value is 0 then it is unticked
If result value is 1 then it is ticked

System Preferences doesn't initiate itself in the Network pane. So, right now, the script will throw an error because window "Network" won't exist.

Hi Ricabude,

Use Run Shell Script action is easy to finish this task.

To check:
networksetup -setwebproxystate "Wi-fi" on
networksetup -setsecurewebproxystate "Wi-fi" on

to uncheck:
networksetup -setwebproxystate "Wi-fi" off
networksetup -setsecurewebproxystate "Wi-fi" off

1 Like

Thanks so much for you all, for your prompt reply. Here's the feedback:

  • the first and second scripts, by @demirtas1 and @CJK only worked, strangely enough, in row 1, it doesn't work for the other rows, and I needed a click on row's 3 and 4. I could see it clicks on the box, but it doesn't mark/unmark the checkbox (except in row 1), not altering thus the proxy settings.

  • the third shell script, by @OscarGong, worked like a charm, with the advantage of setting/desetting proxy even with System Preferences closed, which is very handy.

I've used UI Browser to get the attributes of the checkbox, thanks @DanThomas, but it is indeed quite expensive, though doing a great job. Two thoughts remain:

  • is there a free app to get the attributes of UI's?

  • how can I learn more about shell scripts?

Again, thank you all for your wonderful help

Ricardo

@Ricabude, correction: I didn’t contribute any scripts, they were both from @demirtas1. I only replied to that post to inform him that they wouldn't work because of a clear oversight.

I would have chosen to do it the bash way.

1 Like

If you happen to have Script Debugger, you could use this method:

sorry @CJK, you're right. Thanks again for your help

Just for perfection sake - does anyone know why the command:

click checkbox 1 of row 1 of table 1 of scroll area "Select a protocol to configure:" of group 1 of tab group 1 of sheet 1 of window "Network"

only works in row 1?

Hey Ricardo,

Because accessibility there is broken either by accident or by design.

-Chris

1 Like

I think a simpler solution would be to create a new Location with your desired proxy settings and create a Macro to switch to that location when you are on the work network. I have something similar to switch to a manual IP address on my home network. See below. Melon is my home network.

48%20AM

2 Likes

Thanks a lot Ben.

How do you create a new location?

In the System Preferences, in the Networks panel, you can create locations in the menu at the top.

1 Like

thanks Peter, very helpful