郭日天
郭日天

Reputation: 21

Applescript determine Internet reachability

I am recently experiencing unstable Internet connection I just want to make a program with applescript that reminds me when my Internet is reachable so I can go online. Basically the program checks the Internet by pinging google.com for example. I know I can run shell script using applescript, but the problem is, how to get the return value of the ping and put it in an if statement?

Upvotes: 0

Views: 661

Answers (2)

oa-
oa-

Reputation: 371

I've added two scripts.

  • The first one checks your internet connection once.

  • The second runs in the background every minute and displays a popup if it can reach the internet.

Script 1

    try

        set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"

    on error

        set thePing to "no internet connection"

    end try

Script 2

(*
1. Save as an Application: Script Editor > File > Export… > File Format: Application
2. Check "Stay open after run handler"
3. Run the app or add it to your login items: System Preferences > Users & Groups > User > Login Items > Press the "+" button

# http://stackoverflow.com/questions/43250408/applescript-determine-internet-reachability
*)

on idle
    try
        set thePing to do shell script "/sbin/ping -o -c 5 8.8.8.8"
        if thePing does not contain "error" then
            display dialog "The internet connection is up"
        end if
    on error
        set thePing to "error: no internet connection"
    end try
    delay 60
end idle

Upvotes: 0

vadian
vadian

Reputation: 285290

Pure AppleScript solution:

set testIP to chkUP("http://www.apple.com") or chkUP("http://www.google.com")
if testIP then
    display dialog "Internet Connection is UP"
else
    display dialog "Internet Connection is DOWN"
end if

to chkUP(theURL)
    return (count (get ((theURL as URL)'s host & {dotted decimal form:""})'s dotted decimal form)) > 0
end chkUP

Upvotes: 1

Related Questions