超级n00b到编程世界,但是我已经完成了第一部分。但每当我坐在飞机上或其他我没有上网的地方时,会出现一个错误对话框,告诉我无法连接到VPN。
如何改进此脚本以首先检查互联网连接,然后执行其余操作,但如果没有连接,请不要通知我,但稍后再试?
我的基本VPN连接脚本:
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
非常感谢您提前! :)
答案 0 :(得分:1)
简单而有趣的问题。我在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
您可以在启用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
享受,迈克尔/汉堡