使用 AppleScript 单击应用程序菜单栏项

编程入门 行业动态 更新时间:2024-10-26 12:32:32
本文介绍了使用 AppleScript 单击应用程序菜单栏项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

I use Time Tracker For Mac as my, well, time tracker. It has a menu bar item which I want to be able to access via keyboard shortcut.

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

I found a way to click the item with GUI scripting:

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

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

Unfortunately the script does not return success unless I acted on the menu (i.e. pressing Enter or Esc key). So if I want to trigger the down arrow key...

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

脚本只是永远等待.如果我点击退出,菜单栏项目将关闭,然后才会触发向下箭头键.

the script just waits forever. If I hit escape the menu bar item closes and only then the down arrow key will be triggered.

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

It's kind weird. I just need the menu bar item's click to not block further script execution.

有什么建议吗?

推荐答案

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

The click command returns after about 5 seconds for me. One workaround is to use ignoring application responses and terminate System Events:

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

相关问题:

Applescript:通过 gui 脚本点击菜单栏项AppleScript UI 脚本编写速度是否通常很慢,还是我的脚本或其他原因?

这篇关于使用 AppleScript 单击应用程序菜单栏项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-17 14:37:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/915936.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单击   应用程序   菜单栏   AppleScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!