18

When I wake my machine in the morning, it often stays blank because there's some conflict between my 4k monitor and the 'Your computer name has changed for the 3950th time' dialogue that means both screens are off until I dismiss the dialogue I can't see.

I don't want to have to turn wifi on and off manually - we have computers to do things automatically for us - and I need a wired connection while I'm at my desk.

So can I tell the Mac only to enable to wifi if eth0 is inactive?

Or turn the 'Your computer name has changed' dialogue into a notification that doesn't need manual dismissal, because I really don't care what number Apple has decided to add to it today?

5
  • 8
    Simply change the service priority of the interfaces by moving the Ethernet interface to the top in System Prefs > Network > Gear at the bottom > Set service order...
    – klanomath
    Mar 30, 2017 at 8:00
  • @klanomath Good suggestion. But work said it was time to refresh my laptop and I opted to switch to Linux instead. Much easier than fighting Apple every step of the way. Mar 26, 2019 at 9:46
  • See as well apple.stackexchange.com/questions/98815/…
    – Wollmich
    Feb 15, 2021 at 9:18
  • @klanomath - That's the official solution, but it doesn't work consistently. macOS often does not respect the configured service order. See: apple.stackexchange.com/q/349903/12509 Mar 1 at 15:18
  • "wifi" is spelled "WiFi". Could you correct that?
    – Thinkr
    Mar 7 at 21:21

4 Answers 4

10

You have a couple of solutions:

  1. Automatically disable Wifi when an Ethernet connection (cable) is plugged in on a Mac

    This is a bash script that will automatically turn your wifi off if you connect your computer to an ethernet connection and turn wifi back on when you unplug your ethernet cable/adapter. If you decide to turn wifi on for whatever reason, it will remember that choice. This was improvised from this mac hint to work with Yosemite, and without hard-coding the adapter names. It's supposed to support growl, but I didn't check that part. I did, however, add OSX notification center support. Feel free to fork and fix any issues you encounter.

  2. OS X Yosemite: Disable WiFi if Using Gigabit Ethernet

2
  • 5
    Right now this stackoverflow post is the top Google result for "mac disable wifi on ethernet". This makes your solution #3 moot...
    – Chris
    May 22, 2020 at 2:22
  • 1
    LOL Any google search-based solution is self-evidently moot, especially when it's google.fr (or any other localized search for that matter).
    – Brunox13
    Sep 2, 2020 at 15:43
5

I realise this question is a bit old... however, as there are multiple ways of accomplishing the same result, I thought it worth sharing one more: using Hammerspoon and a couple of snippets of Lua code.

Hammerspoon is a tool to automate various operations on a Mac. I'm not associated to it in any way, I literally installed it to try and achieve the same purpose as the OP.

For anything you want to automate, you write little Lua scripts; in this case, I created a network.lua file which I saved next to the main configuration file, $HOME/.hammerspoon/init.lua. Here's the contents of both:

# init.lua
-- Imports
local homeNetwork = require "network"

-- Toggle WiFi based on Ethernet being connected or not
homeNetwork.setEthernetInterface("USB 10/100/1000 LAN")
systemWatcher = hs.caffeinate.watcher.new(homeNetwork.handleWifi)
systemWatcher:start()

# network.lua
local M = {}
local ethernetInterface

local function isEthernetConnected()
    ipv4 = hs.network.primaryInterfaces()
    activeInterfaceName = hs.network.interfaceName(ipv4)

    return activeInterfaceName == ethernetInterface
end

local function toggleWifi()
    desiredState = not isEthernetConnected() and true or false

    if hs.wifi.interfaceDetails()['power'] ~= desiredState then
        hs.wifi.setPower(desiredState)

        newState = desiredState and "on" or "off"
        hs.alert.show("Turning wifi " .. newState)
    else
        hs.alert.show("Wifi already in the desired state")
    end
end

function M.setEthernetInterface(ethernetInterfaceName)
    ethernetInterface = ethernetInterfaceName
end

function M.handleWifi(event)
    if event == hs.caffeinate.watcher.systemDidWake then
        toggleWifi()
    elseif event == hs.caffeinate.watcher.screensDidUnlock then
        toggleWifi()
    end
end

return M

The only thing you'll have to customise is the name of your Ethernet interface in init.lua; in my case it's "USB 10/100/1000 LAN" because I'm using a dock. You can find the name for yours in the Network section of your System Preferences.

3
  • If you renamed it in the Network section of your System Preferences, it does not use the updated name. You can find the original name in Apple Menu -> About This Mac -> System Report -> Ethernet
    – Jeff
    Apr 25 at 12:41
  • This is one of those examples where I had no idea a tool existed (Hammerspoon, which also seems very popular, awesome!), but it solves so many issues I've been wanting to solve, as a new Mac user using the fantastic new Apple Silicon laptops (M1), coming from Linux. I'm sure you can check the interface type (hammerspoon.org/docs/hs.network.html#interfaceDetails) when plugging/unplugging, instead of hard coding. However, given that most users only have a couple of different docks they use, it's not a massive drama.
    – Josh
    Jun 4 at 9:38
  • From what I can see, interfaceDetails does not return the interface type, unfortunately. For WiFi interfaces, though, it does return a few more keys, namely Airport, CaptiveNetwork and SleepProxyServers. In principle, the check could be implemented by saying "if the primary interface is NOT WiFi" (because, say, the Airport key is not present in the output) "then turn WiFi off". I'll give it a go and, if it seems reliable enough, I'll update the post. Thanks for the suggestion 🙂
    – TataBlack
    Jun 7 at 10:21
2

It hasn't been updated in a while, but ControlPlane handles this very well.

1

Inspired from answer posted by TataBlack, I implemented the same solution using Hammerspoon which is surprisingly still active and well maintained.

Instead of looking for wake/sleep events which is not the ideal scenario, to address the issue at hand, we can use USB connection/disconnection events to trigger a new WiFi check

# init.lua
-- Imports
local homeNetwork = require "network"

-- Toggle WiFi based on Ethernet being connected or not
homeNetwork.setEthernetInterface("USB 10/100/1000 LAN")
systemWatcher = hs.usb.watcher.new(homeNetwork.handleWifi)
systemWatcher:start()

# network.lua
local M = {}
local ethernetInterface

local function isEthernetConnected()
    ipv4 = hs.network.primaryInterfaces()
    activeInterfaceName = hs.network.interfaceName(ipv4)

    return activeInterfaceName == ethernetInterface
end

local function toggleWifi()
    print('checking toggle wifi')
    desiredState = not isEthernetConnected() and true or false

    if hs.wifi.interfaceDetails()['power'] ~= desiredState then
        hs.wifi.setPower(desiredState)

        newState = desiredState and "on" or "off"
        hs.alert.show("Turning wifi " .. newState)
    else
        -- hs.alert.show("Wifi already in the desired state")
    end
end

function M.setEthernetInterface(ethernetInterfaceName)
    ethernetInterface = ethernetInterfaceName
end

function M.handleWifi(usbEvent)
    if string.find(usbEvent['productName'], ethernetInterface) ~= nil then
        hs.timer.doAfter(10, toggleWifi)
    end 
end

return M

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .