Check for active internet connection with Applescript/Automator

10,298

Solution 1

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"

Solution 2

The above script causes automator's applescript to hang if a domain is not available. It works fine in AppleScript Editor if you add -t X where X is a number of seconds ping should time out otherwise it'll hang indefinitely as well.

repeat with i from 1 to 2
    try
        do shell script "ping -o -t 2 www.googleasda.com"
        exit repeat
    on error
        say "Couldn't connect"
        delay 2
        say "Error after delay 5"
        beep
        if i = 2 then error number -128
    end try
end repeat
say "Connected"
Share:
10,298
24fos
Author by

24fos

Updated on June 04, 2022

Comments

  • 24fos
    24fos almost 2 years

    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.