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,
Chris

2 Likes

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

Works! Amazing!

@cstone
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.

-Chris

2 Likes

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?

image

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.

-Chris

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.

-Chris

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:
image

Wifi on:
image

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

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?

-Chris

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