Given the Network Preferences, and AppleScript support for that, I thought this should be straight forward. But I can’t find an AppleScript solution, even though I’ve searched extensively.
I need a handler/function that returns true/false to isEthernetConnected()
Here’s what it looks like in Network Preferences:
CONNECTED:
.
NOT CONNECTED:
.
But I do NOT see this “Status” field/property in the Network Preferences service item.
That almost works, but I had to change it to “1000baseT”.
But I’d really like to have a direct AppleScript or ASObjC solution, if there is one.
Any ideas?
Test Script
tell application "System Events"
tell network preferences
--- no help here ---
set netProp to properties
end tell
tell current location of network preferences
set serviceList to every service -- WORKS
--- this works, but don't see any properties that indicate status ---
set enetService to service "Thunderbolt Ethernet Slot 1"
--- this is "missing value" ---
set enetConfig to current configuration of enetService
--- Unfortunately this is not cleared when the cable is disconnected ---
set macAddr to MAC address of interface of enetService
end tell
end tell
By my reading of System Events’ dictionary, it should be the connected property of a configuration. But with current configuration returning missing value and no configurations showing up as elements of services, my guess is that it has been removed deliberately.
There’s no ASObjC option either; the APIs are all C-based.
If it’s just for your own use, you can probably get something out of ifconfig -au – the -u limits it to connections that are up.
This works here, but obviously you’ll need a wider test base.
on isEtherNetConnected()
set t to do shell script "system_profiler SPNetworkDataType | grep -i 'media subtype'"
if t contains "none" then
return "False"
else
return "True"
end if
end isEtherNetConnected
set f to isEtherNetConnected()
EDIT: though I may have misunderstood your need. This will test whether an ethernet device is connected to the mac (i.e, the ethernet cable is plugged in), but on reflection I expect you want to know if the ethernet connection to the internet is working. You’ll probably need a separate test for that, such as a ping to google.com or the like.
Thanks for your script, but unfortunately, it does not work for me. It returns true even when I disconnect the Ethernet cable.
It looks like you are looking for a match with “media subtype” from the system_profiler output. That returns three matches of “media subtype”, none of which are actually connected.
Here is an example output. I do NOT have a “Thunderbolt FireWire” attached, although the dock I’m using has a FW port.
Thunderbolt FireWire:
Type: FireWire
Hardware: FireWire
BSD Device Name: fw0
IPv4:
Configuration Method: DHCP
IPv6:
Configuration Method: Automatic
Ethernet:
MAC Address: 00:50:b6:10:00:00:5d:31
Media Options: Full Duplex
Media Subtype: Auto Select
Proxies:
Exceptions List: *.local, 169.254/16
FTP Passive Mode: Yes
Service Order: 5
Thanks again for your help.
Thanks Shane. I think that may be the ticket.
It provides an output that includes this:
Almost all connections will have an ether parameter. even virtual ones created by VMware fusion have a MAC address, which is what the ether parameter shows.
in general, the first en* listing is the ethernet port, especially on machines with a dedicated ethernet port. So on my laptop, that’s en0.
on a machine with two, IIRC, those are en0 and en1.
If you want to be sure, you can do a combination of commands. First, assuming all you care about are en* interfaces, you could get a list of all en* interfaces:
ifconfig -au|grep -c en.:
which will return the number of en0/en1 type interfaces.
then a loop that iterates through those applying them to the netstat command:
netstat -S -I en0|grep lqm
That will return a “good” (literally) when the interface is up and off when it is down/no cable plugged in: