Can AppleScript or any other script measure a Macbook's wifi signal strength?

Solution 1:

If your main priority is to have your Mac automatically switch to next strongest access point.....

Your Mac can do that for you already.

Of course you have to have set up both routers as Automatically Join and at the top of your list.

Then tell it to roam and it will automatically connect to next stronger signal network.

Even better is if your SSID and WPA are the same for both.

To turn the roaming on in case it was off do this in Terminal:

sudo defaults write /Library/Preferences/com.apple.airport.opproam enabled -bool true

Then set it up to automatically join next strongest access point

sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport prefs joinMode=Strongest

You are done, now you can move around and your Mac will keep you connected to the strongest signal.

More reading material here

And Apple explains it how it works

If you still prefer scripting this process here is the list of Airport commands /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport

Solution 2:

Here's a JavaScript for Automation (JXA) script that will scan for WiFi networks and retrieve the SSIDs and RSSI values:

ObjC.import('CoreWLAN');
nil = $();

(() => {

    const defaultInterface = $.CWWiFiClient.sharedWiFiClient.interface;

    if (!defaultInterface.powerOn) return false;

    const networks = defaultInterface
                    .scanForNetworksWithNameError(nil,nil)
                    .allObjects;

    const SSIDs = ObjC.deepUnwrap(networks.valueForKey('ssid'));
    const RSSIValues = ObjC.deepUnwrap(networks.valueForKey('rssiValue'));

    const WiFi = SSIDs.reduce((ξ, item, i)=>{ 
                    ξ[item] = RSSIValues[i];
                    return ξ;
               }, {})

    var WiFiByStrength = {};
    Object.keys(WiFi).sort((i,j)=>{ return WiFi[j] - WiFi[i]; })
                     .map(key=>WiFiByStrength[key] = WiFi[key]);

    return WiFiByStrength;

})();

It presents the output of key-value pairs sorted by signal strength (RSSI) in order, starting with the strongest WiFi network signal first:

{"CK.net":-38, "NCC-1701-D":-59, "Peter's Wi-Fi Network":-67, 
"BTWifi-X":-68, "BTWifi-with-FON":-68, "BTHub4-WMJM":-68}

Here, the RSSI values are negative numbers, with a number closer to 0 (more positive) indicative of a stronger WiFi signal.