17

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?

klanomath
  • 64,996
  • 9
  • 126
  • 195
Tim Baverstock
  • 483
  • 1
  • 4
  • 10
  • 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 '17 at 08: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. – Tim Baverstock Mar 26 '19 at 09:46
  • See as well https://apple.stackexchange.com/questions/98815/how-does-the-mac-choose-which-connection-to-use-when-both-wifi-and-ethernet-are – Wollmich Feb 15 '21 at 09:18
  • @klanomath - That's the official solution, but it doesn't work consistently. macOS often does not respect the configured service order. See: https://apple.stackexchange.com/q/349903/12509 – Nick Chammas Mar 01 '23 at 15:18

3 Answers3

10

You have several solutions:

  1. https://gist.github.com/albertbori/1798d88a93175b9da00b

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.

  1. https://macperformanceguide.com/blog/2014/20141118_0958-OSX_Yosemite-disable-WIFI.html

  2. The associated google search :

https://www.google.fr/search?client=safari&rls=en&q=disable+wifi+if+ethernet+osx&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=VL_cWMj1B-zUXt3CkLAM

StrawHara
  • 4,725
  • 3
  • 27
  • 50
  • 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 '20 at 02: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 02 '20 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.

TataBlack
  • 151
  • 1
  • 4
2

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

Brecht Machiels
  • 312
  • 2
  • 11