21

Is there a way to assign the WiFi on/off function to one of the function keys like F4 that I otherwise never use? I assume Applescript would be required. However I may want to use a BASH script instead.

0

5 Answers 5

33

Thanks to budakpisang for this:

In Terminal, run

networksetup -listnetworkserviceorder | sed -n '/Wi-Fi/s|.*Device: \(.*\)).*|\1|p'

to get your WiFi network device (usually it's en0 or en1, depending on the Mac model you have). Substitute it for en1 in the following steps

You can turn wifi off and on with these commands

networksetup -setairportpower en1 off
networksetup -setairportpower en1 on

Here's a one-liner to toggle between on and off

networksetup -getairportpower en1 | grep "On" && networksetup -setairportpower en1 off || networksetup -setairportpower en1 on

Create a keyboard shortcut that runs a shell command

  1. Start Automator, and create a new Quick Action.

  2. Set "Service receives selected: to "no input" in "any application".

  3. Add an action named "Run Shell Script". It's in the Utilities section of the Actions Library.

  4. Insert the bash command you want into the text box and test run it using the Run button (top right). It should do whatever the script does (off, on or toggle), and there should be green ticks below the Action.

  5. Save it, giving it a service name you can remember.

  6. Go to System Preferences -> Keyboard, and go to the Shortcuts tab

  7. Go to the Services section, and scroll down to General - you should find your service there. If you select the line, you can click "add shortcut" and give it a keyboard shortcut.

0
5

A slight improvement on the great answer of Drew Ogryzek worked (better) for me. The following script doesn't make any assumptions on which network adapter is used for WiFi:

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
0

Sharing that great answer onto multiple iCloud devices was unfortunately not as simple as setting it locally. The following answer will work for the expanded use case.

On the first device, export this workflow from Automator to iCloud's default Automator folder. Then open it manually from iCloud on your other Mac. Tell Automator to Duplicate the workflow file, and now when you Save that, Automator contextually can save it to your local Services library, which triggers the workflow to appear to the keyboard shortcut list as before.

Background explanation: I tell Automator to export The night manager's Automator workflow to iCloud. But when I again open and run this in Automator on an older MacOS device, it will not appear in System Preferences' Keyboard "Shortcuts tab" in the Services - General list to receive a key combination. It did on my first device, just as Drew Ogryzek's answer step 7 describes, but because I haven't needed to modify the workflow in any way, the Automator file-menu won't let me save it as anything other than the same file in the iCloud folder. This, however, fails to trigger it to appear to System Preferences.

-1

I just tried it and this worked for me. Using Spotlight launch System Preferences, then begin type wifi and hit enter, use the tab and arrows keys to navigate through the window items until select "wifi" and then "turn wifi on" and thats it. No code but also no hotkeys, just some keyboard navigation.

-3

Just press the wifi key on the keyboard. f12 on US standard keyboard. why are you guys making it too hard.?

3
  • F12 is increase volume see Apple's list of keyboards support.apple.com/en-gb/HT201794
    – mmmmmm
    Aug 17, 2016 at 10:14
  • F12 on windows US standard Keyboard :) Aug 17, 2016 at 10:21
  • This is an Apple based site so Apple keyboards are expected here so we want an answer for that
    – mmmmmm
    Aug 17, 2016 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .