Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigSully/0e59ab97f148bc167ea19dbd42ebef4b to your computer and use it in GitHub Desktop.
Save BigSully/0e59ab97f148bc167ea19dbd42ebef4b to your computer and use it in GitHub Desktop.
toggle system proxy by hammerspoon shortcuts in macos
1. set the ip and port of http and https proxy manually for the first time, which can be accomplished in command line though.
networksetup -setwebproxy "Wi-fi" 127.0.0.1 8080
networksetup -setsecurewebproxy "Wi-fi" 127.0.0.1 8080
2. add the following shell function to your ~/.profile
toggleProxy() {
e=$(networksetup -getwebproxy wi-fi | grep "No")
ns=wi-fi
status=''
if [ -n "$e" ]; then
status=on
else
status=off
fi
echo -n "Turning $status proxy"
networksetup -setstreamingproxystate $ns $status
networksetup -setsocksfirewallproxystate $ns $status
networksetup -setwebproxystate $ns $status
networksetup -setsecurewebproxystate $ns $status
}
3. add the following to your ~/.hammerspoon/init.lua
hs.hotkey.bind({"cmd", "alt", "ctrl"}, "P", function()
local output = hs.execute("toggleProxy", true)
hs.alert.show(output)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment