Check for active internet connection with Applescript/Automator
Asked Answered
A

2

6

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.

Amateur answered 5/11, 2012 at 1:7 Comment(2)
Perhaps you could post some code for others to look at? Also please checkout out how to use Markdown formatting. This will allow you to format the ping command above in a monospaced font - the help link to the right of the edit-box provides a good summaryInterlocutory
thanks for the advice. I"m new to stackoverflow so i'll keep that in mind for the future. The answer below is exactly what I needed, so no need to post anything now.Amateur
D
7

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"
Deliverance answered 5/11, 2012 at 2:0 Comment(0)
A
5

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"
Amplifier answered 10/4, 2014 at 17:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.