【发布时间】:2019-03-21 15:11:08
【问题描述】:
我有一个名为 Fenêtre 的应用程序,当使用 top 命令查看进程名称时,它会给出名称 Fene?~Btre H。
我想点击它的菜单栏项下名为'a.py'的项,如图所示。
我的尝试:
尝试 1
tell application "System Events" to tell process "Fenêtre"
tell menu bar item 1 of menu bar 1
click
click menu item "Show all" of menu 1
end tell
end tell
错误:
$ osascript a.applescript
a.applescript:121:157: execution error: System Events got an error: Can’t get menu item "Show all" of menu 1 of menu bar item 1 of menu bar 1 of process "Fenêtre". (-1728)
请注意,当我只运行 attemp1 的第一行和最后一行时,它运行良好,当我添加中间行时,它无法运行。
尝试 2
ignoring application responses
tell application "System Events" to tell process "Fenêtre"
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 "Fenêtre"
tell menu bar item 1 of menu bar 2
click menu item "a.py" of menu 1
-- click menu item 1 of menu 1 -- another try
end tell
end tell
更新(仍然出现错误)
tell application "System Events" to tell process "Fenêtre"
get entire contents of menu bar 2
end tell
这给出了:
{menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"}
参考:
Applescript: on clicking Menu Bar item via gui script
applescript click menu bar option
https://superuser.com/questions/587815/can-applescript-osascript-be-used-to-click-menu-extra-menu-items
Applescript to show Apple menu bar items
Is AppleScript UI Scripting very slow in general, or is it my script, or something else?
Clicking an applications menu bar item with AppleScript
非常感谢。
【问题讨论】:
-
从您的屏幕截图中,Fenêtre 似乎是一个菜单栏应用。在我在这里使用不同的菜单栏应用程序进行的实验中,实际可见的菜单栏项似乎在
menu bar 2,而不是menu bar 1。但是,对于我正在测试的应用程序,该菜单栏项的内容对辅助功能系统不可见(这是系统事件用来访问 UI 的)。我不知道这对于我正在测试的应用程序是特定的还是对所有菜单栏应用程序都是通用的。你从get entire contents of menu bar 2得到什么? -
tell application "System Events" to tell process "Fenêtre" get entire contents of menu bar 2 end tell给{menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"} -
tell application "System Events" tell (first application process whose bundle identifier is "com.yoannmoinet.fenetre") get entire contents of menu bar 2 end tell end tell给出了同样的结果{menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"} -
是的,我测试的应用程序大致就是这样。这意味着无法通过这种方式访问菜单项。可能是当菜单实际打开时,项目是可以访问的,但我没有测试过。我知道在菜单栏项目上发出
click命令确实会导致它打开,但是在菜单被关闭之前该命令不会完成,因此您无法发出后续命令来查找。使用try和with timeout可能会在菜单仍然打开时重新获得控制权。 -
如果这些提示、建议和评论对您没有任何好处——只是有不可编写脚本的应用程序——你可以看看一个绝对简单的 Foundation 应用程序/脚本(在下面),你可以使用(通过 osascript)单击任何 x/y 屏幕坐标,无论哪个应用程序负责此菜单(项)、按钮或窗口:apple.stackexchange.com/questions/316369/…
标签: macos applescript menubar