If statement 通过AppleScript处理VPN连接

If statement 通过AppleScript处理VPN连接,if-statement,applescript,vpn,If Statement,Applescript,Vpn,为了保持VPN连接处于活动状态,我编写了以下小应用程序脚本: tell application "System Events" tell network preferences connect service "VPNServiceNameIConfigured" end tell end tell 这个脚本很好用! 我给自己写了一个lauchdeamon.plist,在启动、唤醒和每5秒调用一次脚本。这意味着,每当我的VPN连接中断时,它将每隔5秒自动重新连接(如果可能) 这

为了保持VPN连接处于活动状态,我编写了以下小应用程序脚本:

tell application "System Events"
  tell network preferences
    connect service "VPNServiceNameIConfigured"
  end tell
end tell
这个脚本很好用! 我给自己写了一个lauchdeamon.plist,在启动、唤醒和每5秒调用一次脚本。这意味着,每当我的VPN连接中断时,它将每隔5秒自动重新连接(如果可能)

这部分很好,但我想改进一下。我想用一个if-case,比如

if network preferences service "VPNServiceNameIConfigured" is not connected
  connect it
else do nothing
有办法吗?如果是这样的话,我对使用applescript处理系统事件的示例或良好文档感到非常高兴


谢谢大家!

查找该信息的位置在系统事件字典中。您可以使用AppleScript编辑器的“文件”菜单中的“打开词典…”打开任何词典

您没有提供足够的信息来编写准确的代码;例如,您的VPNServiceNameIConfigured服务是否包含任何配置

如果您可以获得配置,您应该能够检查该配置的“已连接”。例如:

if connected of current configuration of service VPNServiceNameIConfigured is false then
    connect it
end if
根据您的设置,您可能还可以检查服务VPNServiceNameIConfigured的“active”布尔值。以下是一个简单的测试脚本,可用于我的设置,以检查我的WiFi是否处于活动状态:

tell application "System Events"
    tell network preferences
        set myConnection to location "Automatic"
        --get name of every service of myConnection
        set myService to service "Wi-FI" of myConnection
        --get properties of myConnection
        if active of myService is false then
            display dialog "Need to reconnect"
        end if
    end tell
end tell

“连通的”布尔值仅在配置中可用,但是,如果您的服务包含配置,这可能是您更可靠的选择。

谢谢Jerry Stratton,我不确定您所说的配置是什么意思-抱歉。我使用网络管理器和hide.io上的指南创建了一个VPN连接。因此,现在在我的网络管理器中,有一个在常见的WLAN条目下,我的HideIO条目,当我单击它时,我可以连接或断开与配置的VPN的连接。我将尝试了解您的示例是什么样的,并向您报告!非常感谢!如果您获得服务的属性,它将告诉您是否有当前的配置附加到它。在您的情况下,获取服务“VPNServiceNameIConfigured”的属性。