Switching WiFi On and Off

Hello, I'm trying to build a simple macro that would switch my wifi on and off. Do I need to use Shell Script for that?

Looks pretty easy judging by this http://brettterpstra.com/2014/02/15/quick-tip-automating-you-macs-wifi-power/

[Edited due to no shebang line in the script.]

Brett hasn’t given a real toggle; he’s just given the pieces to build one.

Try this:

#!/usr/bin/env bash

device=$(networksetup -listallhardwareports | grep -E '(Wi-Fi|AirPort)' -A 1 | grep -o en.)
[[ "$(networksetup -getairportpower $device)" == *On ]] && v=off || v=on
networksetup -setairportpower $device $v

Run it from an Execute Shell Script action in KM.

Best Regards,


[quote=“ccstone, post:3, topic:803”]
#! /usr/bin/env bash…[/quote]

Works! Amazing!

While this works, why can I not record my mouse clicking on the Wifi icon?

I've just tried that, but nothing was recorded.

Hey Tony,

No idea.

I recorded it just now, and it worked fine.

That said – I would not just click – I would Click at Found Image.



Thanks. Would you explain why using Click on Found Image is a better choice?
When I re-created the macro, and changed Click to Click On Found Image, then ran that macro. KM complained that no image was specified.

I'm running Sierra, BTW.

Hey Tony,

Because menu icons can move around on macOS.

You took a screenshot of of the menu icon and pasted it into the image well in the KM action?


Press 4

Select your image capture area while holding down the ⌃ Control key.

Paste into the image well.

NOTE – The action in the Keyboard Maestro Editor MUST NOT be visible when you RUN the macro, because under some circumstances that can prevent the desired image from being unique.

You can turn on the “Display” checkmark to cause Keyboard Maestro to highlight the found image and turn it back off when you've got the macro working.


No, I didn't . I didn't know that was required.
I just recorded my mouse movements, before I found out about the shell script to toggle wifi.

I thought you had changed to a Click On Found Image action and tried it without success as you mentioned above (see quote below):

In any case – now you know.


Tonight I tried this on Big Sur 11.3.1 and it frickin =SINGS=.
It's 2-3 times faster than even using the Finder's own menubar icon.
Dodging also the strange new "wifi jail" GUI quirk. This made my night. Thanks, Chris.

1 Like

This works very well for me:
Wifi off:

Wifi on:

-- wifi off
do shell script "networksetup -setairportpower en1 off"
-- wifi on
do shell script "networksetup -setairportpower en1 on"

Hey Martin,

Why are you running shell scripts from AppleScript, when Keyboard Maestro will run those commands natively using the Execute a Shell Script action?


Ah. These were from the time I knew nothing about Shell script. I used them for a while without looking at the scripts. I should have caught it when I pasted them here. Haha~
Thanks for pointing that out!

1 Like