So those of you with a Mac OS X who also use the handoff feature will have possibly experienced a bug with trying to answer calls on the Mac when you have multiple interfaces configured.

I connect to my home network using the MacBooks onboard airport card and also use a thunderbolt ethernet adapter when sat at my desk. The problem lies when I get back to my desk I have to manually disable the airport card and when leaving my desk I have to manually reenable it again… this drives me insane. Something this simple should be easy to configure on OS X, well it turns out it is!

Using AppleScript it’s possible to detect the state of an ethernet adapter using a few shall scripts. As such you can then detect the state and use this to toggle the WiFi on/off using networksetup command.

Here’s the code that’s required:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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};
do
        networksetup -listallhardwareports | grep ${i}
done
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
        else
            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

But to make your life easier I’ve created an app for you here:
https://www.phillips321.co.uk/downloads/InterfaceToggle.app.zip

When you’ve got the app running you can tell it to “Open at Login” using the dock option.
Screen Shot 2015-08-05 at 19.14.21

Leave a Reply