The following AppleScript code will return all the network services in the Network Setup.
tell application "System Events"
tell current location of network preferences
set names to get name of every service
end tell
end tell
How do I get only the active network service name instead of all?
Update with clarification: 'active' in this context means the network service to whose interface the current [primary] IPv4 address is bound.
@mcgrailm's answer shows you how to obtain all active (read: not disabled) network services, whether currently bound to addresses or not.
This answer, by contrast, shows you how to determine the network service to whose interface the current (primary) IPv4 address is bound:
Unfortunately, this is non-trivial, as the information must be drawn from several sources:
services
objects (from dictionarySystem Events.sdef
) contains no information about currently bound addresses.service
object'sinterface
property contains aMAC address
property.IPv4 address of (system info)
returns the current (primary) IPv4 address, sadly,primary Ethernet address of (system info)
always returns the wired Ethernet interface's MAC address, which may or may not be the interface to which the current IPv4 address is bound.do shell script
to parse the output from CLIifconfig
with help fromawk
.