How to create keyboard shortcut for WiFi toggle? [duplicate]

I want to create keyboard shortcut for Turning ON WiFi when it is turned OFF, and vice versa.


Solution 1:

Step 1: Find out device ID of Wi-Fi

$networksetup -listnetworkserviceorder | grep Wi-Fi
(2) Wi-Fi
(Hardware Port: Wi-Fi, Device: en0)

In this case it is en0

Step 2: Create a "Quick Action" using Automator.app. Set "no input" in "workflow receives" in "any application". Drag and drop "Run Shell Script" and add the following shell script to the text field.

if ( networksetup -getairportpower en0 | grep -q Off ); then 
 echo "network is off. turning ON";
 networksetup -setairportpower en0 on 
else echo "network is ON. turning OFF"; 
 networksetup -setairportpower en0 off
fi

Save the file in automator as, say, wifi-toggle

Step 3: Set keyboard shortcut by going to Keyboard -> Shortcuts -> Services

Scroll down. Look under "General". You should see an entry for "wifi-toggle"