Prevent Application From Opening if Connected to Any WiFi Network?

I want to prevent a certain app from opening if my computer is connected to Ethernet, or in other words, this app should only open if I'm connected to WiFi and Ethernet is disabled. I've got strict requirements at my work with the IT department, and I can't have a certain app opening if I'm connected to the company's internal Ethernet network.

Currently, instead of reaching around the back of my iMac and plugging/unplugging the Ethernet cable manually, I set up KM macros to toggle my WiFi/Ethernet connection by turning off and disabling one, then enabling and turning on the other. Here's a screenshot of what things look like when I'm connected to only Ethernet:


You can see that all other network connections are disabled and greyed out – only Ethernet is enabled and active. When I activate my KM macro to move to WiFi, it looks the same except the WiFi connection is enabled and active, and the Ethernet connection is greyed out and disabled. Sorry, I hope that makes sense and that I'm not annoyingly over-explaining things. :slight_smile:

So, again, I'd like to be able to tell KM to prevent a certain app from activating or opening while I'm on Ethernet. Is something like that possible?

You have two choices to prevent the app from launching or activating using a KM Macro:

  1. Disable the Macro (manually or via Action)
  2. Deactivate the Macro Group (by Action) the Macro is in.

Of course this won't stop the App from being manually activated/launched. So you will need a separate Macro that is triggered by either of those events to quit if it happens.

Now, the hard part: How to Detect if your Mac is connected using WiFi. I use this bash script to return "ON" or "OFF":

Script to Get WiFi Status

#!/usr/bin/env bash

device=$(networksetup -listallhardwareports | grep -E '(Wi-Fi|AirPort)' -A 1 | grep -o en.)
[[ "$(networksetup -getairportpower $device)" == *On ]] && wifiStatus=ON || wifiStatus=OFF
echo $wifiStatus

Here's a starter macro.
You need to change:

  1. Trigger -- Wireless Network trigger
  2. Everywhere I have used "BBEdit", you need to choose your app and Macro Group.

MACRO:   Determine if WiFi is On [Example]

~~~ VER: 1.0    2018-07-13 ~~~


Determine if WiFi is On [Example].kmmacros (4.8 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.




Nice shell script, @JMichaelTX.

In case the OP or anyone else is curious of other methods to obtain the Wi-Fi status, here's an AppleScript method:

use application "System Events"

get the description of the ¬
	first menu bar item of the ¬
	first menu bar of the ¬
	application process "SystemUIServer" where ¬
	the description contains "Wi-Fi"
--> ➀ "Wi-Fi, No Signal." (On, not connected)
--> ➁ "Wi-Fi, four of four bars, with %SSID%." (Connected)
--> ➂ "Wi-Fi, Wi-Fi is not enabled." (Off)
(the result contains "bars") as integer
--> 1 (connected); 0 (not connected/off)

This, however, only works if you've set the Wi-Fi symbol to be shown in the menu bar (most people do).

You can also use AppleScriptObjC:

use framework "CoreWLAN"

property CWInterface : a reference to current application's CWInterface

set currentInterface to CWInterface's interface()
currentInterface's powerOn() --> true (on); false (off)
currentInterface's ssid() as text --> missing value = disconnected
1 Like

D'oh *facepalm


Wow, thanks guys for the awesome responses! I'll definitely give these a go!