Applescript点击状态栏应用程序菜单项

6

我有一个名为Fenêtre的应用程序,在使用top命令查找进程名称时,它给出了名称Fene?~Btre H

我想要点击菜单栏中的“a.py”项目,如下图所示。

enter image description here enter image description here

我的尝试:

尝试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:通过gui脚本单击菜单栏项目
applescript点击菜单栏选项
https://superuser.com/questions/587815/can-applescript-osascript-be-used-to-click-menu-extra-menu-items
Applescript显示Apple菜单栏项目
AppleScript UI脚本一般很慢吗,还是我的脚本或其他原因?
使用AppleScript单击应用程序的菜单栏项目

非常感谢。


从您的截图来看,Fenêtre似乎是一个菜单栏应用程序。在我对另一个菜单栏应用程序进行实验时,实际可见的菜单栏项目似乎在“菜单栏2”而不是“菜单栏1”中。但是,对于我测试的应用程序,该菜单栏项目没有任何内容对辅助功能系统可见(这是System Events用于访问UI的内容)。我不知道这是否特定于我正在测试的应用程序还是所有菜单栏应用程序的通用情况。您从“获取菜单栏2的全部内容”中得到了什么? - Ken Thomases
tell application "System Events" to tell process "Fenêtre" get entire contents of menu bar 2 end tell gives {menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"} - user8864088
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 gives the same thing {menu bar item 1 of menu bar 2 of application process "Fenêtre" of application "System Events"} - user8864088
是的,这大致是我测试的应用程序得到的结果。这意味着菜单项不能通过这种方式访问。可能是当菜单实际打开时,可以访问这些项目,但我还没有测试过。我知道在菜单栏项上发出“click”命令会导致它打开,但该命令直到菜单被关闭才完成,因此您无法发出后续命令以了解情况。使用“try”和“with timeout”可能有效,可以在菜单仍然打开时恢复控制。 - Ken Thomases
如果这些提示、建议和备注都对你没有帮助——确实有一些无法编写脚本的应用程序——你可以看看一个非常简单的基础应用程序/脚本(在下面),你可以使用它(通过osascript)点击任何x/y屏幕坐标,无论哪个应用程序负责此菜单(项)、按钮或窗口:https://apple.stackexchange.com/questions/316369/is-there-a-terminal-command-to-open-a-mac-menu-bar-item/342158#342158 - clemsam lang
1个回答

2
请使用 bundle identifier 代替应用名称:
tell application "System Events"
    tell (first application process whose bundle identifier is "BUNDLE_IDENTIFIER_HERE")
        tell menu bar item 1 of menu bar 1
            click
            click menu item "Show all" of menu 1
        end tell
    end tell
end tell

1
也可以尝试使用类似于“菜单项2”这样的东西,而不是使用名称或“返回菜单1的全部内容”来确定其中的内容。 - pointum
1
如何找到捆绑标识符名称?“BUNDLE_IDENTIFIER_HERE”。索引无效。 - user8864088
1
我尝试了 ... bundle identifier is "com.yoannmoinet.fenetre",但仍然出现了 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 application process 1 whose bundle identifier = "com.yoannmoinet.fenetre". 的错误。 - user8864088
2
为了找到捆绑列表,我使用了/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Fenêtre.app/Contents/Info.plist - user8864088
这个答案是正确的并且有效!你所需要做的就是更改选项名称并在 /Applications/<app> 文件夹中的 Info.plist 中找到捆绑标识符。 - mourodrigo
那些不在 /Applications 目录下的应用程序怎么办,比如只出现在菜单栏上的小型应用程序? - volvox

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接