【问题标题】:Using AppleScript to navigate menu bar item使用 AppleScript 导航菜单栏项
【发布时间】:2018-06-18 21:31:36
【问题描述】:

我制作了一个 AppleScript 来与菜单栏项 (NordVPN) 进行交互。基本上,它单击项目,选择连接或断开连接,仅此而已。

在开发这个时,我遵循了here回复中的一些建议

它工作了几次,但现在它只是挂起并保持“运行”。什么都没有发生。我想知道忽略响应是否有问题?这样做是为了防止点击之间出现 5 秒的延迟。或者这两次尝试会导致问题吗?我正在尝试确保脚本运行,无论是“连接”还是“断开”。

任何建议都是有帮助的。如果有人对更好的方法提出建议,我将不胜感激。谢谢

代码如下:

ignoring application responses
tell application "System Events" to tell process "NordVPN IKE"
    click menu bar item 1 of menu bar 2
end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "NordVPN IKE"
tell menu bar item 1 of menu bar 2
    try
        click menu item "Connect" of menu 1
    end try
    try
        click menu item "Disconnect" of menu 1
    end try
end tell
end tell

编辑:现在它又可以工作了。它似乎在某些时候有效......但我无法弄清楚为什么它在其他时候停止工作。

编辑 2: 问题似乎出现在 Mac 进入睡眠状态时。当我唤醒它并尝试运行脚本时,它挂起。但是,如果我手动单击菜单栏项然后运行脚本,它将起作用。

【问题讨论】:

    标签: macos applescript freeze menubar


    【解决方案1】:

    这适用于我使用最新版本的 macOS high Sierra。也许这段代码对你来说会更好一些。

    set disconnectExists to false
    set connectExists to false
    
    ignoring application responses
        tell application "System Events"
            launch application "NordVPN IKE"
            delay 1
            click menu bar item 1 of menu bar 2 of application process "NordVPN IKE"
        end tell
    end ignoring
    do shell script "killall System\\ Events"
    tell application "System Events"
        repeat until disconnectExists or connectExists is true
            set disconnectExists to menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 ¬
                of application process "NordVPN IKE" exists
            set connectExists to menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 ¬
                of application process "NordVPN IKE" exists
        end repeat
        try
            if connectExists is true then
                delay 0.2
                click menu item "Connect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
                    application process "NordVPN IKE"
            else if disconnectExists is true then
                delay 0.2
                click menu item "Disconnect" of menu 1 of menu bar item 1 of menu bar 2 of ¬
                    application process "NordVPN IKE"
            end if
        end try
    end tell
    

    【讨论】:

      猜你喜欢
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多