Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Use AppleScript to Query internet connection

I'm new to AppleScript and was looking to have a script that can alert me when my network status changes. I'm wanting this to help diagnose issues when I'm home and away and trying to be a little creative and need some assistance.


What I'm looking for is a script that will display a message on the screen when the network is down (for example unable to connect to apple.com) and will say The Network is Offline and will disappear once it's back online. But As the netowrk comes back online I also want it to shoot me a text notifying me of such an event and have my computer say the network is back online. I would like to have this run non stop in the background if possible so it will need to be repeating assuming this doesn't put any kind of strain on my imac.




My current code bits:


--sends a text to myself

tell application "Messages"

send “Network is back online" to buddy "XXXXXXXXXX" of service "E:myemail@apple.com"

end tell



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 "Network is back online"




This isn't much and I can't even get these two bits working together but figured someone else may have an interest in this as well and despite multiple searches I can't find anything exactly like this and I'm not to the point where I could take something similar and make it work for me yet.



as a side note I do have it set to sleep after an 3 hours and do have the wake function enabled and my computer does have a password lock. if that's an issue I'd appreciate help around that as well or I may use any scripts provided and just go from there.

Posted on Dec 19, 2013 1:19 PM

Reply
2 replies

Dec 19, 2013 8:11 PM in response to JMANTN

You might want to look at ControlPlane as a way to monitor your network, and then have an "Action" that does something to alert you.


Normally I would use ControlPlane to switch my network settings when I move from one location to another, as it knows how to watch your network for changes.


Maybe you can take advantage of this to alert you of a network problem as well. Just an idea.

Dec 27, 2013 1:22 PM in response to BobHarris

Here's possibly a better explanation of what I'm trying to attempt:


I'm trying to write a Script that checks if internet is online. When it's off alert me. Then alert me when back online with a text and continue to monitor connection every 2 min if its online or however long would be good.

Here's my code:


on idle

repeat

try

if (do shell script "ifconfig en0 | awk '/inet/ {print $2}'") is not equal to "" then

--Only try to ping if Internet is detected

try

do shell script ("ping -W1 -c1 apple.com")

on error

--At least one packet was dropped

display dialog "I'm on Online, but Apple.com didn't respond to a ping"

end try

end if

on error

--Internet is no-longer active

display dialog "Network is Offline"

repeat until (do shell script "ifconfig en0 | awk '/inet/ {print $2}'") is not equal to ""

--Repeat this section until network is back online

try

if (do shell script "ifconfig en0 | awk '/inet/ {print $2}'") is not equal to "" then

--Only try to ping if Internet is detected

try

tell application "Messages"

--It should only send a text once since it can't send until network is back

send "Network is back online" to buddy "XXXXXXXXXX" of service "E:XXXXX@gmail.com"

end tell

on error

--error

say "Something's Wrong"

end try

end if

end try

delay 120

end repeat

exit repeat

end try

delay 120

end repeat

end idle



What this does however when saved as an application is every two minutes alert me it couldn't reach apple.com and that's it. Does nothing when offline or when network comes back online.

I've pieced this together from several sources and no dice.

Use AppleScript to Query internet connection

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.