Toggle Network → Ethernet → Advanced → Proxy On / Off

Hello, I often toggle network pref → Ethernet → advanced → proxies → automatic proxy proxy on / off , which entails a lot of clicking. Is there a way to create a macro which would do so, or at least get me as close as possible to the final click ?
thank you

Try this to see if it will work for you.

Sorry for the repost a few times. :slight_smile:

zzzz.kmmacros (22 KB)

1 Like

Hey @ronald,

Try the create-network-location solution first.

Then you can use Keyboard Maestro's Set Network Location action as necessary.

-Chris

1 Like

WOW !! fantastic. Very smart. thanks very much. Your macro will improve my workflow and save time.

I just have to add an action to type or paste the URL, which is no problem. I will simply duplicate your ingenious move and click action to click in the URL box and add an action to type the URL.

The proxy is necessary for me at times, but has the disadvantage of slowing down the Internet access.

The problem is that because my IQ is in the lower range (between orang outang and chimpanzee), I tend to forget if I recently turned the proxy on or not, meaning that I never remember if the proxy is on or off.

I wonder if it is conceivable to create a macro which would do only one thing: answer proxy on or proxy off. The problem is how to 'read' the proxy status. Would you have an idea ?

thanks again very much

image

very interesting. I will look into it.
I will ask you the same question as kcwhat: I am constantly forgetting if my proxy is on or off. With your solution, do you have any idea if I could write a macro which simply display a notification (proxy on / off), or even (crazy question) display it in the menu bar ?.
thank you very much !

Hey @ronald,

Use a shell script in combination with any of several little utilities that can display information in the menu bar.

BitBar

AnyBar

TextBar

-Chris

1 Like

thank you.

TextBar is my app of choice for putting stuff in the menu bar. The developer has been very responsive to questions.

BitBar was a flash-in-the-pan a few years ago but hasn't seen much development since then.

AnyBar would probably work better if I wasn't "color challenged" (not to mention forgetting what the colors were supposed to mean).

Another potential solution.

Would it happen to be true that you only want to use certain apps with the proxy?

The reason I ask is that I have Keyboard Maestro enable / disable my http / https proxy when I launch / quit Proxie.

When it launches, Keyboard Maestro does this:

	/usr/sbin/networksetup -setwebproxystate Wi-Fi on
	/usr/sbin/networksetup -setsecurewebproxystate Wi-Fi on

when it quits, Keyboard Maestro does this:

	/usr/sbin/networksetup -setwebproxystate Wi-Fi off
	/usr/sbin/networksetup -setsecurewebproxystate Wi-Fi off

I just thought that might be worth mentioning, because then you wouldn't have to worry about remembering to turn the proxy on or off manually, it would just happen when the app you want to use with the proxy is launched or quit.

If you run into problems, do post a followup. I'll be glad to help if I can.

1 Like
"Wi-Fi"

In the command will be different for different connections.

-Chris

1 Like

Good point. You might need to use Ethernet instead of Wi-Fi if you are using a wired connection.

1 Like

@ccstone @tjluoma
thanks very much for the suggestions.
@tjluoma I will buy textbar and try to implement your solution. Very useful in terms of workflow.

Thank you for a very intelligent suggestion. The problem is that the app is chrome. Is there a simple way to narrow it down to certain web sites ? Just asking. Please don't spend more time on this. You have done a lot already.

I installed textbar. Sorry, I am confused. Now what do I do if I want to display both the Ethernet and WiFi proxy status on the menu bar ?
thank you

Unfortunately I don't think there is a way to limit it to websites within Chrome, but there is another option is "Epichrome" which will allow you to make a browser based on Chrome that you use for particular websites.

1 Like

I will look into it. thank you

Ok, so I'm going to explain how this is done, and then at the end I'll put it all together into a script that you should be able to use with TextBar. If you want to skip the explanation part, that's fine.

You need to check 4 things:

  1. http proxy status on Ethernet
  2. https proxy status on Ethernet
  3. http proxy status on Wi-Fi
  4. https proxy status on Wi-Fi

That requires 4 shell commands

networksetup -getwebproxy Ethernet
networksetup -getsecurewebproxy Ethernet
networksetup -getwebproxy Wi-Fi
networksetup -getsecurewebproxy Wi-Fi

Each of those commands will show something like this if enabled:

Enabled: Yes
Server: localhost
Port: 4321
Authenticated Proxy Enabled: 0

or this if disabled:

Enabled: No
Server: localhost
Port: 4321
Authenticated Proxy Enabled: 0

Note that localhost and 4321 are used for Proxie and might be different for your setup, so don't worry if that looks different.

However, the only part that we really care about is the first line, that starts with the word "Enabled" and ends with either "Yes" or "No".

So, we can use awk to filter the output of the networksetup command to give us just the part we care about:

networksetup -getsecurewebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}'

The -F' ' tells awk "look for spaces". If it had been -F':' it would have told awk to "look for colons".

The /^Enabled:/ says "Look for a line that starts with the word "Enabled:" Note that I used the ^ to indicate "the first word in the line" because we don't want to match Authenticated Proxy Enabled: just Enabled:

Then {print $2} means "print the second word after the match (the match being "Enabled:" and the space being the "split point").

In this case you could also use {print $NF} because $NF tells awk to "use the very last thing on the line. (Why $NF? I have no idea. I assume there was a reason years ago.)

So, if we modify the four commands with our new awk addition, it looks like this:

networksetup -getwebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}'
networksetup -getsecurewebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}'
networksetup -getwebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}'
networksetup -getsecurewebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}'

Now let's assign those each a variable name:

ETHERNET_REGULAR=$(networksetup -getwebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}')
ETHERNET_SECURE=$(networksetup -getsecurewebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}')
WIFI_REGULAR=$(networksetup -getwebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}')
WIFI_SECURE=$(networksetup -getsecurewebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}')

Now we can refer to each of those using:

$ETHERNET_REGULAR
$ETHERNET_SECURE
$WIFI_REGULAR
$WIFI_SECURE

Each one will either be "Yes" or "No" depending on the state of the proxy.

Now, if we are willing to simplify things a bit, we can imagine three different scenarios:

  1. All variables are "Yes"
  2. All variables are "No"
  3. Some are "Yes" and some are "No"

Obviously you could get a lot more nuanced/granular than what is contained in the 3rd option: You could say "ETHERNET_REGULAR is No but all the other are Yes" and so on, and so on.

But what we want to know, generally speaking, is this: is the proxy ON (for all of the above) or OFF (for all of the above)?

We can test for that fairly easily using an "if / else" conditional:

This line tests to see if all of the variables equal "Yes"

if [ "$ETHERNET_REGULAR" = "Yes" -a "$ETHERNET_SECURE" = "Yes" -a "$WIFI_REGULAR" = "Yes" -a "$WIFI_SECURE" = "Yes" ]

Note that capitalization is important. "Yes" and "yes" and "YES" are different. The -a in between each means "and" so that is saying:

$ETHERNET_REGULAR equals Yes AND ETHERNET_SECURE equals Yes AND $WIFI_REGULAR equals Yes AND $WIFI_SECURE equals Yes

Now we need another test to see if they are all equal to "No"

elif [ "$ETHERNET_REGULAR" = "No" -a "$ETHERNET_SECURE" = "No" -a "$WIFI_REGULAR" = "No" -a "$WIFI_SECURE" = "No" ]

Note that I use elif instead of if this time. elif just means "else if" in shell programming. We don't want to check both of these, because if they all equal "Yes" then we don't need to check if they all equal "No". That's just basic logic, right?

The second test (the elif line) is checking to see if all the variables equal "No" instead of "Yes"

Now for the third option, we just need an else

So let's put that all together:

Skip to here if you don't care about the explanation of how this works

Here's a shell script you can use with TextBar (note: I saved this as a Github gist too: (download link below)

#!/bin/zsh -f

PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

	## Note:
	## ETHERNET_REGULAR / WIFI_REGULAR means http
	## ETHERNET_SECURE / WIFI_SECURE means https

ETHERNET_REGULAR=$(networksetup -getwebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}')
ETHERNET_SECURE=$(networksetup -getsecurewebproxy Ethernet | awk -F' ' '/^Enabled:/{print $2}')
WIFI_REGULAR=$(networksetup -getwebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}')
WIFI_SECURE=$(networksetup -getsecurewebproxy Wi-Fi | awk -F' ' '/^Enabled:/{print $2}')

if [ "$ETHERNET_REGULAR" = "Yes" -a "$ETHERNET_SECURE" = "Yes" -a "$WIFI_REGULAR" = "Yes" -a "$WIFI_SECURE" = "Yes" ]
then

	echo "Proxy ON"

elif [ "$ETHERNET_REGULAR" = "No" -a "$ETHERNET_SECURE" = "No" -a "$WIFI_REGULAR" = "No" -a "$WIFI_SECURE" = "No" ]
then

	echo "Proxy OFF"

else

	echo "Proxy Mixed\nEthernet http: $ETHERNET_REGULAR\nEthernet https: $ETHERNET_SECURE\nWiFi http: $WIFI_REGULAR\nWifi https: $WIFI_SECURE"

fi

exit 0

The else clause uses "Proxy Mixed" as the first line, and then puts the status of each of the 4 conditions as a new line underneath it ("\n" in echo means "new line").

Text Bar will show the first line of the output in the menu bar, and if you click on it, it will show the rest of the lines in a drop-down menu, so if you get a "Proxy Mixed" message, you can click on it and see what is on and what is off. ("Yes" means "the proxy is on" and "No" means "the proxy is off".)

The easiest way to work with a complex shell script with Text Bar is to put it into an external file such as /usr/local/bin/proxystate.sh and then tell TextBar to use that file.

So download this file and save it as /usr/local/bin/proxystate.sh and then in Terminal.app do:

chmod 755 /usr/local/bin/proxystate.sh

and then add it to TextBar as shown here:

UPDATE:

I have updated the gist to look to see if the Ethernet variables are empty, which will happen if you are using a MacBook/MacBook Air/MacBook Pro which does not have a wired Ethernet port. If those variables are empty, the script will just report the Wi-Fi status for the proxy, meaning that the gist version can be used on a MacBook as well as an iMac/Mac mini/Mac Pro (which I assume will have Ethernet ports).

3 Likes

Unfortunately Epichrome destroys the AppleScript dictionary in the resulting app, so it can't be scripted.

But -- that may be good enough for some folks.

-Chris

2 Likes

That is unfortunate, but my thought was more for Keyboard Maestro automation, as in:

  1. When this Epichrome app launches, enable the proxies

  2. When this Epichrome app quits, disable the proxies

If that's all that's needed and AppleScript is not, it might still be a viable option.

1 Like

Holy Moses !! Thank you so much for your detailed explanation and all the time you spent on the script and your post. I am extremely grateful. Please give me a few hours to read through the explanation and test in Textbar. Once again thank you very much !!

I want to move the .sh file to /usr/local/bin/ but can't find that directory.
thank you