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.

up vote 19 down vote accepted

Thanks to budakpisang for this:

In terminal, you can turn wifi off and on with these commands

networksetup -setairportpower en1 off
networksetup -setairportpower en1 on

en1 being wifi. Try networksetup -listnetworkserviceorder to see whether that's right for you or not.

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 Service.

  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.

  • Thanks. That's a little more involved that I'd like. I reinstall my OS periodically to wipe away any malware infections. Doing all those steps each time would be tedious. I'll file a feature request with Apple for a switch or something. – Ebu Jul 4 '15 at 5:31
  • 1
    Note that on some machines (e.g. late model Macbook Pro Retina), the WiFi network is eth0. – Daniel Griscom Jul 6 '15 at 2:05
  • 1
    @Ebu You can copy over this folder to keep your services: ~/Library/Services – juil Feb 14 '17 at 1:19

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

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.

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

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

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.