有人在使用 IKEv2 网络服务时体验过 AppleScript 吗?
在 El Capitan 中,我可以创建 IKEv2 VPN 连接并正确连接。但是 AppleScript 不适用于这种连接/服务,它无法获取具有名称的服务,也无法列出来自服务的连接。
tell application "System Events"
tell current location of network preferences
set service_name to "IKEv2_connection_name"
do shell script (do shell script "scutil --nc start \"" & service_name & "\"")
end tell
end tell
这是错误:
error "System Events got an error: No service" number 1
AppleScript 似乎无法识别 IKEv2 VPN 连接。所以我尝试运行另一个脚本来打印系统中所有当前的互联网连接:
tell application "System Events"
tell current location of network preferences
set names to get name of every service
end tell
end tell
结果显示所有网络连接(包括“Wi-Fi”、“USB 以太网”、“蓝牙 PAN”、“Thunderbolt Bridge”、L2TP、PTPP、IPSec 类型的所有 VPN 连接),但未列出任何 IKEv2 连接,尽管我已经设置了其中一些并且它们都正常工作。
最佳答案
我能够使用 UI 脚本使其正常工作。到目前为止,这个脚本似乎运行良好,并且我认为我已经使其尽可能对用户友好。它要求始终打开系统偏好设置,但可以隐藏该窗口。如果脚本启动时系统偏好设置已关闭,则脚本将启动,然后自动隐藏系统偏好设置。主要的不便在于,在脚本运行时,“系统偏好设置”本质上被锁定到“网络” Pane 。欢迎尝试一下,如果您有任何问题或建议,请告诉我!您需要将“我的 VPN 服务名称”替换为您想要保持连接的 VPN 服务的名称。您可能还想修改底部的延迟。
repeat while true
tell application "System Events"
tell application process "System Preferences"
if not (window 1 exists) then
tell application "System Preferences"
activate
end tell
repeat while not (menu bar 1 exists)
end repeat
repeat while not (menu "System Preferences" of menu bar 1 exists)
end repeat
repeat while not (menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1 exists)
end repeat
delay 3
click menu item "Hide System Preferences" of menu "System Preferences" of menu bar 1
end if
click menu item "Network" of menu "View" of menu bar 1
tell window 1
repeat while not (rows of table 1 of scroll area 1 exists)
end repeat
repeat with current_row in (rows of table 1 of scroll area 1)
if value of static text 1 of current_row contains "MY VPN SERVICE NAME" then
select current_row
exit repeat
end if
end repeat
repeat with current_button in (buttons in group 1)
if name of current_button is equal to "Connect" then
click current_button
exit repeat
end if
end repeat
end tell
end tell
end tell
delay 60
end repeat
关于applescript - 如何通过 AppleScript 获得 IKEv2 VPN 连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39091587/