Turn on/off Wifi using Keyboard shortcut

Mac users can turn on/off Wifi using option available as part of the Menu bar.

Wifi Menu bar option

Add quick action in Automator app

Launch Automator app on your mac and choose Quick Action.

Automator – Quick Action

Choose action as “Run Shell Script” then set Workflow receives to “no input” and set it to run for for any application and select an image for your action.

Then under the run script window add the following script.

set_wifi_on_or_off() {
  networksetup -getairportpower en${n} | grep ": ${1}";
  if test $? -eq 0;
  then
    echo WiFi interface found: en${n};
    eval "networksetup -setairportpower en${n} ${2}"
    return 0;
  fi
  return 1;
}

for n in $(seq 0 10);
do
  if set_wifi_on_or_off "On" "off"; then break; fi;
  if set_wifi_on_or_off "Off" "on"; then break; fi;
done

You can test the above script using “Run” option and check if Wifi settings turned On or Off based on the current state. Save this quick action by providing a name as “Wifi”

Assign a keyboard shortcut for quick action

Launch Keyboard preferences using System preferences and navigate to Shortcuts tab.

Keyboard Preferences

The newly added Wifi quick action will be available under Services. Now assign a keyboard shortcut for this action.

Note :- Make sure to select 3 letters or more keyboard combination for your shortcut. Sometimes other apps might be using the same set of key combination and this would prevent launching the Automator action.

References