【问题标题】:Clicking an applications menu bar item with AppleScript使用 AppleScript 单击应用程序菜单栏项
【发布时间】:2013-05-22 00:55:52
【问题描述】:

我使用Time Tracker For Mac 作为我的时间跟踪器。它有一个菜单栏项,我希望能够通过键盘快捷键访问它。

我找到了一种使用 GUI 脚本单击项目的方法:

tell application "System Events" to tell process "Time Tracker"
    click menu bar item of menu bar 2
end tell

不幸的是,除非我在菜单上操作(即按 Enter 或 Esc 键),否则脚本不会返回成功。所以如果我想触发向下箭头键...

tell application "System Events" to tell process "Time Tracker"
    click menu bar item of menu bar 2
    -- hangs here forever until menu is closed
    tell application "System Events" to key code 124
end tell

脚本永远等待。如果我点击转义,菜单栏项会关闭,然后才会触发向下箭头键。

有点奇怪。我只需要单击菜单栏项阻止进一步的脚本执行。

有什么建议吗?

【问题讨论】:

    标签: applescript


    【解决方案1】:

    对我来说,单击命令在大约 5 秒后返回。一种解决方法是使用忽略应用程序响应并终止系统事件:

    ignoring application responses
        tell application "System Events" to tell process "Time Tracker"
            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 "Time Tracker"
        tell menu bar item 1 of menu bar 2
            click menu item 2 of menu 1
        end tell
    end tell
    

    相关问题:

    【讨论】:

    • 非常感谢。这确实有效。我不太愿意完全终止该进程,但我会尝试一段时间。
    • 根据Daemons and Services Programming Guide,loginwindow 在注销时向后台进程和守护进程发送 TERM 或 KILL 信号。系统事件支持突然终止,因此即使发送 KILL 信号也应该是安全的。
    【解决方案2】:

    实际上,向下箭头的键码似乎是 125。试试这个:

    tell application "System Events" to tell process "Time Tracker"
        click menu bar item of menu bar 2
        key code 125
        key code 36
    end tell
    

    点击菜单栏...命令后有短暂的延迟(几秒钟),我不知道为什么。

    【讨论】:

      猜你喜欢
      • 2019-03-21
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2013-06-05
      • 2017-05-15
      相关资源
      最近更新 更多