Auto disable/enable - macOS WiFi / Thunderbolt Ethernet Adapter

Hello, I need some help with a script.

After a long search I found a website on the internet where a script has been published which gives me the possibility to terminate the Wifi automatically when connecting my Thunderbolt Ethernet adapter. If I remove it, Wifi will be activated again automatically.

on idle
    tell application "System Events"
        -- identifies the wifi device
        set airPortDevice to do shell script "/usr/sbin/networksetup -listallhardwareports | awk '{if($3=="Wi-Fi"){getline;print}}' | awk '{print $2}'"
        set airPortPower to do shell script ("networksetup -getairportpower " & airPortDevice & " | awk '{print $4}'")
        -- Bashscript to check number of ethernet interfaces
        set NumOfEthernetInterfaces to "EthernetInterfaces=`networksetup -listnetworkserviceorder | grep Ethernet | grep Device | cut -d: -f3 | awk '{$1=$1};1' | sed -e s/\\)//`
for i in ${EthernetInterfaces};
        networksetup -listallhardwareports | grep ${i} 
exit 0"
        set EthernetInterfaces to do shell script NumOfEthernetInterfaces
        if (do shell script NumOfEthernetInterfaces) > 0 then
            if airPortPower is equal to "On" then
                set {gave up:gaveUp} to display dialog "Ethernet found, turning wireless off" with title "InterfaceToggle" buttons {"OK"} giving up after 1
                say "Ethernet found, turning wifi off"
                do shell script "networksetup -setairportpower " & airPortDevice & " off"
            end if
            if airPortPower is equal to "Off" then
                set {gave up:gaveUp} to display dialog "Ethernet not found, turning wireless on" with title "InterfaceToggle" buttons {"OK"} giving up after 1
                say "Ethernet not found, turning wifi on"
                do shell script "networksetup -setairportpower " & airPortDevice & " on"
            end if
        end if
        return 10
    end tell
end idle

The developer also provides a free app, which also works fine under macOS Mojave.

If I now want to use the script published there with KM, I don't know how to determine the right trigger for it to monitor the connection of the Thunderbolt Ethernet adapter or activate the Lan port if necessary.

When I export the script Via the editor into a program, it only works if I activate it (Mouse Click)
What can I do here so that my script/program recognizes that the Thunderbolt Ethernet Adapter is plugged in?

Thank you very much for the help.

There may be a way to do this in Keyboard Maestro, but if there is, I don't know what it is.

The only way I know how to monitor for network configuration changes is with a launchd plist which looks for changes in the directory /Library/Preferences/SystemConfiguration/. The important part is this bit:


I have a shell script /usr/local/bin/ which runs whenever that directory changes.

The /usr/local/bin/ file can contain whatever you want, including calling a Keyboard Maestro macro. Just make sure it's executable (chmod 755 /usr/local/bin/

The full .plist looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

That is saved as ~/Library/LaunchAgents/com.tjluoma.on-network-change.plist.

The only thing to know is that the directory sometimes changes when there's no discernable change in the network setup. I'm not sure why this is, but I assume there's something happening that I'm just not aware of.

Anyway, if there is a more efficient way to monitor network changes that would work in Keyboard Maestro, I'd love to know what it is!

1 Like

Thank you @tjluoma for your detailed answer. I'll give it a try.
It would be so easy to use KM to monitor the connection of the Thunderbolt Ethernet Adapter :disappointed:

Well, as I said, there may be another way. I don't have a Thunderbolt device to test with, so I can't say specifically.

1 Like

@appleianer, from my rudimentary understanding of the way messages are sent throughout the system, I would very much recommend @tjluoma's excellent suggestion and, if there is a situation where the script is called needlessly in response to a directory change that hasn't actually occurred, finding a way around that specific case rather than discarding the method as a whole.

The AppleScript you found is basically establishing itself as a Stay Open application that stays idle in the background and runs every time a set period of time has elapsed (in this case, every 10 seconds). This is polling, which I understand is not an efficient means of monitoring device status in a case like this, meaning it will potentially be an expensive script to run in terms of system resources versus what I have to estimate as its perceived value in achieving its goal.

launchd—for reasons I don't fully understand—is very suited to this sort of activity and results in a lower demand upon the system.

PS. There's, of course, no reason you can't utilise the functional parts of your AppleScript and have launchd execute those as required. You're not limited to shell scripts with launchd.

1 Like

Thank you very much for your detailed answer @CJK :+1::clap: