How to decide if the running system is connected to a network or not.

Hello

I am really puzzled.
I never connected a mac to a network so my question may be dumb.

I am trying to help a user.
The script in progress is supposed to behave differently if it is running as a single machine or when it is running to a given network.

The user said that there are two volumes on the network, say inline1 and inline2 and if the mac is physically connected to the network, the script must execute
mount volume (insert the inline1 descriptor)
mount volume (insert the inline2 descriptor)

When it’s not physically linked to the network, the script open disk images to mount two image disks named inline1 and inline2.

I may code :


tell application "System Events"
	set volumesAvailable to name of disks
end tell
set onNetwork to true
set inline1missing to volumesAvailable does not contain "inline1"
set inline2missing to volumesAvailable does not contain "inline2"
if inline1missing then
	try
		#mount volume (insert the inline1 descriptor)
	on error
		set onNetwork to false
	end try
end if
if onNetwork and inline2missing then
	try
		mount volume (insert the inline1 descriptor)
	on error
		set onNetwork to false
	end try
end if

if not onNetwork then
	# here we aren't connected to the network
	if volumesAvailable does not contain "inline1" then
		tell application "System Events"
			open disk item (path:to:the:image:inline1.sparsebundle)
		end tell
	end if # volumesAvailable 1
	if volumesAvailable does not contain "inline2" then
		tell application "System Events"
			open disk item (path:to:the:image:inline2.sparsebundle)
		end tell
	end if # volumesAvailable 2
end if # not onNetwork

It may work but it I know that it requires several minutes to enter the correct part of code if the machine is not physically connected to the network.
Is there a more efficient automatic scheme ?
Of course, I may use a dialog box:

if machine alone then
click button [alone]
else
click button [networked]
end if

Yvan KOENIG (VALLAURIS, France) jeudi 26 février 2015 16:43:45

Hello Yvan,

getLocation() is an universal handler to check the network connection.
It returns:

connected boolean true if connected
wired boolean true if connected via Ethernet, false if not connected or connected via WiFi
ssid string the name of the SSID if connected via WiFi, otherwise empty string
IPAddress string the IPv4 address of the active connection, otherwise empty string

as the device nodes may differ from machine to machine, there are two properties to specify the primary Ethernet and WiFi nodes


property primaryEthernetDevice : "en0"
property primaryWiFiDevice : "en2"

set {connected, wired, IPAddress, ssid} to getLocation()

on getLocation()
	set wiredIP to do shell script "/sbin/ifconfig " & primaryEthernetDevice & " | /usr/bin/awk '/inet / {print $2}'"
	if wiredIP is not "" then
		return {true, true, wiredIP, ""}
	else
		set wirelessIP to do shell script "/sbin/ifconfig " & primaryWiFiDevice & " | /usr/bin/awk '/inet / {print $2}'"
		if wirelessIP is not "" then
			set ssid to do shell script "/usr/sbin/networksetup -getairportnetwork " & primaryWiFiDevice & " | sed 's/Current Wi-Fi Network: //g'"
			return {true, false, wirelessIP, ssid}
		else
			return {false, false, "", ""}
		end if
	end if
end getLocation

THANKS

I will send a test script to be sure that it works on the asker’s machine.

My imac is connected to the box used to reach the Internet by a cable Ethernet so the handler returns :
{true, true, myWiredIP, “”}

Will ask the user what he get when it is connected and when it’s not.

Yvan KOENIG (VALLAURIS, France) jeudi 26 février 2015 19:24:47