我有一个使用 AppleScript 自动连接到 VPN 的脚本,我该怎么做才能让它只在我连接到互联网时才尝试连接?
I have a script to automatically connect to VPN with AppleScript, how do I make it so it only tries to do it if I'm connected to the internet?
对编程世界超级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
非常感谢您! :)
简单但有趣的问题。我在 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
享受吧,迈克尔/汉堡
对编程世界超级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
非常感谢您! :)
简单但有趣的问题。我在 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
享受吧,迈克尔/汉堡