Help with Network Ping AppleScript

I’m trying to make an AppleScript wait until there’s a successful ping of a local IP or URL before continuing. I want this to work with no WiFi or ethernet and basically be retrying until one of those connections is up and running.

The problem is that “do shell script” returns an error of unknown host if there’s no network connection and breaks my script.

Here’s my starting point script:


set ping to do shell script "ping -c 2 -q google.com"

repeat until ping contains "2 packets received"
	
	delay 1
	
end repeat
display dialog "Connection Successful." buttons {"OK"} default button 1

I’ve tried adding “try” commands and “repeat until” commands but nothing will get around that error.

ANY help is much appreciated!

Something like this?


repeat
	try
		set ping to do shell script "ping -c 2 -q google.com"
		if ping contains "2 packets received" then exit repeat
	end try
	delay 1
end repeat
display dialog "Connection Successful." buttons {"OK"} default button 1

If you turn off WiFi, try this script, it will fail. The shell error overtakes the script.

I’m looking to go from WiFi off, run script and have it able to wait without failing, and then connect WiFi.

Update: estockly! You came to my rescue. I was almost certain I’d tried your exact code and it didn’t work for me. But I must’ve missed something because you’re appears to work. Waits on no network and whenever the connection happens it displays the message. JUST what I’ve been needing to improve a bunch of scripts I’m using for automation.

Thank you!

Is there a way to add a customizable timeout to this script? Meaning it would try to ping for say 10 seconds and if it’s unsuccessful return DIALOG A and if it’s successful return dialog B?