【问题标题】:Handle VPN Connections via AppleScript通过 AppleScript 处理 VPN 连接
【发布时间】:2014-08-26 15:15:17
【问题描述】:

为了让我的 VPN 连接保持活跃,我写了这个小脚本:

tell application "System Events"
  tell network preferences
    connect service "VPNServiceNameIConfigured"
  end tell
end tell

这个脚本运行良好! 我给自己写了一个 lauchdeamon .plist 来在 StartUp、WakeUp 和每 5 秒调用一次脚本。这意味着,每次我的 vpn 连接中断时,它会每 5 秒自动重新连接(如果可能)。

这部分工作正常,但我想稍微改进一下。我想使用类似的 if-case

if network preferences service "VPNServiceNameIConfigured" is not connected
  connect it
else do nothing

有没有办法做到这一点?如果是这样,我对使用 applescript 处理系统事件的示例或良好文档感到非常高兴。

谢谢!

【问题讨论】:

    标签: if-statement applescript vpn


    【解决方案1】:

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

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

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

    if connected of current configuration of service VPNServiceNameIConfigured is false then
        connect it
    end if
    

    根据您的设置,您还可以检查服务 VPNServiceNameIConfigured 的“活动”布尔值。这是一个简单的测试脚本,可用于我的设置以检查我的 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 上的指南创建了一个 VPNConnection。所以现在在我的网络管理器中,在常见的 WLAN 条目下,我的 HideIO 条目,当我点击它时,我可以连接或断开配置的 VPN。我将尝试弄清楚您的示例是什么样的并报告!非常感谢!
    • 如果您获得服务的属性,它应该会告诉您是否有附加的当前配置。在您的情况下,“获取服务“VPNServiceNameIConfigured”的属性”。
    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 2015-07-10
    • 2017-04-13
    • 2016-08-10
    • 2012-04-22
    • 2015-02-20
    • 2023-03-08
    • 2011-12-21
    相关资源
    最近更新 更多