Hi, I want to set a macro to turn WiFi on and off. Seems simple, but my macros never get past opening System Preferences even when I add a Pause Until before the Network window opened - even a mouse click does not do it. I admit I am new at this, what should I do?
Whenever you have this sort of problem, search for it in Google and add the word “AppleScript” (or @ccstone likes “os x shell” as well). So search for:
AppleScript turn off wifi
That immediately finds the shell command networksetup and these helpful commands:
networksetup -getairportpower en1
networksetup -setairportpower en1 off
networksetup -setairportpower en1 on
Which will get the current status of the wifi, and turn the port to it on or off.
See the Apple Support Community Script to turn wifi on/off post for some info (it uses AppleScript, but there is no need to use AppleScript just to run a shell script within Keyboard Maestro, but there is no harm in using an AppleScript either).
Searching the KM forum for ‘wifi’ might be helpful.
Thankyou both of you. I found the Bash script, pasted it into to the “Execute Shell Script” macro and it worked perfectly.
I was looking for this same action!
Would mind sharing this macro?
This is the BASH script:
#!/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
Paste into the “execute shell script” window.
[ 2017.01.15.2032 CST – Post #6 edited to properly display the bash script. -ccs ]