Reputation: 13
Super n00b to the programming world, however I have done the first part. But every time I'm sitting in an airplane or somewhere else where I don't have an internet connection, an error dialog box shows up telling me it's impossible to connect to the VPN.
How can I improve this script to first check for internet connection, then execute the rest, but if there's no connection don't inform me about it, but try again later?
My script for basic VPN connecting:
on idle
tell application "System Events"
tell current location of network preferences
set myConnection to the service "Private Internet Access"
if myConnection is not null then
if current configuration of myConnection is not connected then
connect myConnection
end if
end if
end tell
return 120
end tell
end idle
Thank you very much in advance! :)
Upvotes: 0
Views: 1730
Reputation: 2282
Simple but interesting question. I found this nice solution at MacScripter:
on check_net()
try
set the_URL to "http://www.apple.com" as URL
set dotted_ to dotted decimal form of host of the_URL --> this will return the IP address if there is a live connection
return true
on error
return false
end try
end check_net
You can call the handler before enabling the VPN:
on idle
if check_net() then
tell application "System Events"
tell current location of network preferences
set myConnection to the service "Private Internet Access"
if myConnection is not null then
if current configuration of myConnection is not connected then
connect myConnection
end if
end if
end tell
end tell
return 120
else
-- return another value if you want to wait longer if no internet is available
return 120
end if
end idle
Enjoy, Michael / Hamburg
Upvotes: 1