Location specific macros — If 'WiFi with given name is available' Then

Hi!

I'd like to run a macro periodically If, and only If, my MacBook is at home. The way that is normally proposed to achieve this is to use a Wireless Network Condition, to make the macro run If the home network is connected. But as I sometimes like to work on my computer without being connected to the internet, a better solution for me would be a If-statement for when a given WiFi network is available, as in within WiFi range, but not necessarily connected.

I thought there might be some apple script or something (that I could then run every couple of minutes), that would return a list of all available WiFi networks, but my online searches have not given me any working solutions.

Anyone having any idea about how to do this available WiFi trick? Or any ideas, other than connected WiFi network, on how to make a macro be location specific?

This is how I would structure a KM macro to do this:

  1. Run the macro every so often using a Periodic trigger
  2. In an Execute a Shell Script action run this shell command:
    /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s
    saving the result to a variable
  3. Search the contents of the variable for the WiFi SSID of interest.
  4. If the SSID is found, do whatever actions you wish
  5. Otherwise terminate the macro.

On my system, running the shell command results in something like this:

                            SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                        SKYH3X5J 50:70:43:f2:97:42 -76  1       Y  -- WPA2(PSK/AES/AES) 
                       VM9141472 20:b8:2b:4f:75:ae -86  108     Y  NL WPA2(PSK/AES/AES) 
                       VM7480275 ac:f8:cc:60:d4:52 -89  108     Y  GB WPA2(PSK/AES/AES) 
                      Bd Devices ea:cb:ac:35:f2:72 -84  100     Y  GB WPA2(PSK/AES/AES) 
                        Bd Wi-Fi e2:cb:ac:35:f2:72 -82  100     Y  GB WPA2(802.1x/AES/AES) 
                              R1 b6:b9:8a:4c:89:d3 -23  5,+1    Y  -- WPA2(PSK/AES/AES) 
                        SKYA6E2W 00:a3:88:42:55:aa -67  1       Y  -- WPA2(PSK/AES/AES) 
                        SKYA6E2W 00:a3:88:42:55:ad -77  36      Y  GB WPA2(PSK/AES/AES) 
                              R2 b0:b9:8a:4c:89:d5 -25  48      Y  -- WPA2(PSK/AES/AES) 
                              R3 b0:39:56:7b:32:db -64  48      Y  -- WPA2(PSK/AES/AES)

So for example searching for whether Bd Wi-Fi is in range is pretty simple.

Note: I'm on Catalina, so YMMV.

Here's what the shell command looks like in KM (it takes a few seconds to run BTW)

image

2 Likes

Hi, @Alexander. I can confirm that @tiffle's suggested approach would also work on Ventura 13.5.1.

If you want to put the SSIDs in a text variable, you could do something like this:

Download: Available Wifi SSIDs.kmmacros (5.0 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.5.1 (22G90)
  • Keyboard Maestro v10.2

2 Likes

@Alexander, here's another potential approach...

If you have the Shortcuts application on your Mac, you could use a shortcut to retrieve your current location. The following macro uses the shortcut depicted below:

Current Latitude and Longitude from Shortcuts.kmmacros (5.5 KB)


You can download the shortcut here: Current Latitude and Longitude for KM

1 Like

Wow, thank you, this is great!
It was exactly something like this I was hoping might exist. It also works great in OS Ventura!

1 Like

This is also an interesting approach! But I thought the Mac only retrieved it's location from what WiFi-network it was connected to? Or does the Mac's location services have more tricks up it's sleeve? Like communicating with my iPhones GPS or something? (Not that I am sure I would necessarily want to share my iPhone's location with my Mac though)

@Alexander, correct. Scratch that idea. :grinning:

Thought I'd share my finished macro that returns the status of whether my MacBook is at home or not.
Set up like this to only run the somewhat slow script in the rare cases I am disconnected from my WiFi.
It will however, of course, not return a correct value if the WiFi is switched off, not only disconnected.

I will also redo my other location specific macros (that for instance mutes my Mac's audio at the library) so that I do not have to connect to a network for them to be set into effect. This is great!

MacBookHomeStatus Macro (v10.2)

MacBookHomeStatus.kmmacros (8.0 KB)

Macro image

Or realised that a better solution would be for the macro to keep it's status unchanged if the WiFi is switched of, so made a v1.1 to address this:

MacBookHomeStatus v1.1 Macro (v10.2)

MacBookHomeStatus v1.1.kmmacros (11 KB)

This is a good approach but sadly in Mac OS Sonoma that command line returns

WARNING: The airport command line tool is deprecated and will be removed in a future release.
For diagnosing Wi-Fi related issues, use the Wireless Diagnostics app or wdutil command line tool.

The wdutil command "helpfully" redacts all useful information so it's less than worthless.

wdutil also requires sudo, so use this instead:

networksetup -getairportnetwork en1 | sed 's/.*Network: //'

That assumes your wireless is on en1, which it normally is. But you can check with this:

networksetup -listnetworkserviceorder

-rob.

If you are on KM 11 you shouldn't need these workrounds -- see this post for details, plus that thread has a bunch of ways that do work in Sonoma should you still be on KM 10 (or are having issues with KM 11).