Check for active internet connection with Applescript/Automator

24fos picture 24fos · Nov 5, 2012 · Viewed 9.8k times · Source

I have an Automator workflow to ping a server, and download the latest copy of a schedule that I frequently use. This schedule then is copied to my dropbox so I can view it on my phone. Before the workflow downloads the newest schedule it deletes the old schedule from dropbox.

This works well, except when I don't have an active internet connection. When I don't have an active internet connection, the workflow will still open up dropbox, delete the old schedule, and try to download the newest one. Because there is no connection, it doesn't download anything. Then if my connection becomes active the empty dropbox will sync and the schedule will be deleted from my phone.

I'm trying to add a few lines of applescript code to ping a server to see if i have an active connection. If I don't, then wait about 5 seconds and ping again. I want to have 5 ping attempts and at that point if i still do not have an active connection then I want to quit entirely.

I'm very new to applescript, so I'm getting hung up on how to handle an error from a command, in this case, the ping. If command "ping -o www.apple.com" fails, wait 5 seconds and retry the ping. If 5 failed attempts then quit entirely.

Answer

adayzdone picture adayzdone · Nov 5, 2012

Maybe something like this?

repeat with i from 1 to 5
    try
        do shell script "ping -o www.apple.com"
        exit repeat
    on error
        delay 5
        beep
        if i = 5 then error number -128
    end try
end repeat
say "Connected"