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.


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
  • 2
    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;
    echo WiFi interface found: en${n};
    eval "networksetup -setairportpower en${n} ${2}"
    return 0;
  return 1;

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

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

You must log in to answer this question.

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