Is Network Computer ON?

I run Applescripts within Microsoft Word that transfer documents between my Mac Pro 2019 and my wife’s iMac 2019 over LAN. But when the iMac is not on, the script hangs up Word for about a minute before it generates an error message.

Please provide me with a script to check to see if her iMac is on before attempting to send/receive files. All help is appreciated!

Interesting question, but I’ve never tried testing remote access on a real-world remote computer. Therefore, the following has not been tested by me. This is just my guess. I myself will be interested to know if this works:


try
	set remoteComputerIsOn to exists machine "eppc://192.163.1.2.3"
on error number -1728
	set remoteComputerIsOn to false
end try

An alternative shell script would be to use the ‘networksetup’ command line tool, to get info about your ethernet connection.
Like this.


set ethernetInfo to do shell script "networksetup -getinfo ethernet"

Which will give you a list of info about the ethernet ports connection.
If their is an ethernet connected system, then you will get a different info list, of which there should be an ‘IP address’, which you can retrieve like this.


set ethernetIPAddress to do shell script "networksetup -getinfo ethernet | grep 'IP address' | awk -F ':' '{print $NF}'"

Not tested, as I have no other system available at present to connect to the ethernet port.

Regards Mark

How do you connect to your wife’s computer? Does it show up in the sidebar when it’s connected (under one of the sharing types, e.g. bonjour, connected servers)? There should be some info that would help determine a simple and reliable method of detection, like a hostname or IP address.

Thank you all for your help. I am in the middle of something. I will try all and report back.

I connect my wife’s computer using ethernet and it does show up in Finder. But I have to open finder each time, scroll down to the Network section to see if she’s logged in.

But I do this a few times a day and it has become annoying. It would be nice for my Applescript/VB macros to not try the operation if her system is OFF.

Guys, thanks again for taking the time to provide solutions. Here’s what I found:

KniazidisR’s solution returned "Can’t get machine “eppc…” error

Mark FX’s solution returned “ethernet is not a recognized network service…” error


Fredrik71 solution which was the simplest worked:

set remoteComputerIsOn to do shell script “ping -c 1 192.168.1.71”

Returns:

"PING 192.168.1.71 (192.168.1.71): 56 data bytes
64 bytes from 192.168.1.71: icmp_seq=0 ttl=64 time=0.423 ms

— 192.168.1.71 ping statistics —
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.423/0.423/0.423/0.000 ms"

As you can see I modified it to ping only once. If remoteComputerIsOn contains “0 packets received” it means my wife’s computer is OFF.

Thank you again everyone!

I jumped the gun! Pinging works in the script editor but not in word. Since it takes about 10-15 seconds for Applescript to return the ping results when the remote system is OFF, I get an error message in Word!

Can I change the routine so the script waits 3" for the ping results then aborts the shell command if nothing returned yet?

Thank you Fredrik! The addition of the error trapping code did the trick for me. I didn’t even need your dialog box. Here’s the final Applescript which is inside a file called “networking.scpt”:

on IsRemoteComputerON(return_value)
try
set return_value to do shell script “ping -c 1 127.0.0.2”
on error the error_message
set the return_value to "Error: " & ". " & the error_message
return the return_value
end try
end IsRemoteComputerON

Here’s the calling VB script function in Microsoft Word:

Function IsRemoteComputerON() As Boolean
Dim return_value As String
return_value = AppleScriptTask(“Networking.scpt”, “IsRemoteComputerON”, return_value)
IsRemoteComputerON = InStr(return_value, “0 packets received”) <> 0
End Function

According to Microsoft rules Networking.scpt must reside in the following folder:
~/Library/Application Scripts/com.microsoft.word

IsRemoteComputerON() returns True if my wife’s computer is ON. It takes 11 seconds on my system to get a response for the shell operation if her computer is OFF. It takes about a second to get a response if her system is ON.

Thank you again for your help Fredrik!

Fredrik, you’re the man! You actually answered my previous question about the timeout! The addition of your timeout switch worked like a charm! The Word VBScript returns and error message after about 2.5" when the remote computer is OFF.

Thanks again for everything!