I suppose I fall into the "if your only tool is a hammer" cliché… shell scripts are where I am most comfortable.
I'm not even sure how to use the code that you've provided. Is that ruby or swift or something else?
UPDATE: Oh, it's AppleScript! (Another language that I wish I knew better.) That's handy to know. Thanks!
UPDATE 2: OK, so that AppleScript is handy, but all it does it get the SSID. The shell script I wrote does a bit more than that.
If all you want to do is get the current SSID in a shell script, you can do this in a "one-liner":
airport -I | awk -F': ' '/ SSID/{print $NF}'
Now, I have linked /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
to /usr/local/bin/airport
so I can use it more easily, but if you haven't done that, you'll need to use the full path to airport
.
But the other thing that airport
does it report whether or not the AirPort/Wi-Fi card has been turned off or not. (The AppleScript just says "missing value" which is the same as what it says when there's no Wi-Fi network connected.)
If Wi-Fi has been turned off for some reason, then the script won't do anything because it assumes that was done on purpose.
However, if Wi-Fi is on but not connected to a Wi-Fi network then the script will power-off the AirPort card, wait a few seconds, and then turn it back on. That will often be enough to cause it to rejoin a local Wi-Fi network if it dropped off of it for some reason.
It is also capable of determining the proper Ethernet port for the Wi-Fi (for example, on a MacBook it's probably en0 but on an iMac or Mac Mini it's probably en1). This is handy because I actually sync this macro to a MacBook running Mojave, a Mac Mini running High Sierra, and an iMac running El Capitan.
This is handy if you have a Mac that runs as a headless server in the basement or in an entertainment center that is not connected by Ethernet where it would be a pain to have to hook up some kind of monitor and keyboard in order to manually re-connect to the Wi-Fi network.
The latest change I made was to add a slight delay to the beginning of the script, because if you are changing Wi-Fi networks in the same location for some reason, I found that it was interrupting that process. So I added a brief sleep
and then check to see if it is already re-joined a Wi-Fi network, and if it has, then it won't bother with any of the above, because the whole point is to make sure that the computer stays on a Wi-Fi network.
Anyway, that's why I wrote it as a shell script, because I wanted to include this level of complexity to it, and shell scripting is the language that I know best. I'm quite sure you can probably do it in other languages too, but if someone doesn't know any scripting at all, they ought to be able to use my script just by "dropping it in" to Keyboard Maestro.