Quick Tip: Automating your Mac’s wifi power
Quick tips are random posts regarding something I discovered on my way to something bigger. They usually get longer than “quick” would imply, for which I refuse to apologize.
This is a quick one, and many of you probably know it, but you can quickly toggle wifi on your Mac with the networksetup
command. To turn your wifi on:
networksetup -setairportpower en0 on
and to turn it off… you guessed it:
networksetup -setairportpower en0 off
To make it even easier, add an alias in your shell like:
alias wifi="networksetup -setairportpower en0"
Then you can toggle it just by typing wifi on
or wifi off
. Note that, depending on your network interfaces, en0 may need to be changed. You can find out what all of your network hardware addresses are with networksetup -listallhardwareports
. Just look for the labeled “Wi-Fi.”
You can also use the command in AppleScripts:
do shell script "networksetup -setairportpower en0 on"
I turn my MacBook Air’s wifi off with ControlPlane when I’m docked to my Thunderbolt Display (it has an ethernet line). I’m using the above script with UnDock to turn it back on before I disconnect (ControlPlane would only do that afterwards).
With my wifi set to a higher priority than ethernet in Network Preferences, this allows me to make fairly seamless network switches without any mousing around. Hyper-Escape triggers UnDock, which starts up wifi, umounts my drives and pauses Time Machine. Then, unplugging the Thunderbolt cable triggers tasks (via ControlPlane) such as hiding GeekTool in preparation for a 1440px desktop, repositioning Simplify/Sidecar and other handy things. Of course, it also does the whole process in reverse when I plug back in.
Just thought some other rabbit-holers might find this useful.