OS X Safari: Toggle Socks Proxy from command line

command linePROXYsafarisocks

I need to toggle back and forth between engaging and disengaging a socks proxy within Safari:

Next open safari and click on "safari" in the top left menu bar to
open the drop down menu and then click on preferences. You can do the
same thing by pressing CMD+,. Click on the advanced tab and find
"Proxies", click "Change Settings". System Preferences should open and
you can now select "SOCKS Proxy". You should be setup and if you check
google for your IP it should be masked by the address that you're
SSH'd into. Note this is system wide and will cover all network
communications. You can turn this off by unselecting the SOCKS Proxy.

Is it possible to perform this task with a script? Bonus round: add button or menu item to Safari UI to execute proxy toggle. Any feedback to clarify question is appreciated.

Best Answer

To turn on SOCKS proxy for the network service called Wi-Fi, run

networksetup -setsocksfirewallproxystate Wi-Fi on

Replace ‘Wi-Fi’ with the name of your network service.
Replace ‘on’ with ‘off’ to turn off the proxy.