2

I have read at multiple places that the folder /Library/Preferences/SystemConfiguration is supposed to "change" in case that I unplug a network cable.

Now, I have a MacBook Pro, so I use a USB3 adapter to connect a network cable.

Indeed, nothing changes when I plug or unplug it within this folder. I clearly see in "Network settings" that the LAN connection is established or dropped when I plug or unplug the cable.

Where should anything change? Is there some folder or anything else that I can use as trigger for AppleScript, when plugging/unplugging the network cable?

3
  • What exactly are you trying to do when this occurs? Are you trying to script an ability to turn on your wifi? I've scripted that in the past with bash but shouldnt be hard to do in AppleScript. – ʀ2ᴅ2 Sep 27 '18 at 2:42
  • @ʀ2ᴅ2 question is, HOW can I start some AppleScript when the network cable is (un)plugged – IceFire Sep 27 '18 at 6:23
  • There's no file that gets written when you bring up/down a network interface. This is a state that's handled by the networking kexts. To see this in action, use Terminal and type sudo dmesg. Then disconnect your network. sudo dmesg again to see to see the disconnect event. Plug back in and sudo dmesg again to see the connection message. As far as programmatically getting access to this, I don't believe AppleScript has this ability. – Allan Sep 27 '18 at 18:04
0

Save this script as an application and set it to run on startup. It will use some memory but the script is very light. You can delay even more. Mind that this routine will keep detecting the cable forever, so you must set other conditions inside your script to do what you need and stop until something changes.


on run {input}
    repeat
        set input to do shell script "ifconfig -a" as text
        set status to offset of "status:" in input
        set the_starting_point to status + 0
        set the_ending_point to status + 13
        set isCableConnected to characters the_starting_point thru the_ending_point of input as string
        if isCableConnected is "status: active" then
            display dialog "Ethernet On"
        else
            display dialog "Cable Unplugged"
        end if
        delay 1
    end repeat
end run

3
  • A background endless loop? – IceFire Sep 27 '18 at 17:41
  • Yup. You need something to keep tracking the status of the cable. – Mateus Ribeiro Sep 27 '18 at 17:43
  • Unless you remove the repeat and use a shortcut or some other trigger. – Mateus Ribeiro Sep 27 '18 at 17:43
0

This project in Github does what you are looking for - look in there to see how he is doing it. https://github.com/paulbhart/toggleairport

He watches these three files to determine that the network status has changed:

  • /private/var/run/resolv.conf
  • /Library/Preferences/SystemConfiguration/NetworkInterfaces.plist
  • /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist

The purpose of his project is to turn off WiFi if the LAN is connected, and turn it back on again if the LAN is disconnected.

-1

Maybe you can use a shortcut to activate your script since you are already plugging the cable anyway. Just create a new Service for finder using Automator, drag a Run Applescript action to the workflow, place your code inside, save it and set a shortcut under Preferences/Keyboard/Shortcuts/Services.

1
  • I would like to run such a script when the network cable is (un)plugged. How to react to this event is my question, not how to write the script – IceFire Sep 27 '18 at 5:44

You must log in to answer this question.

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