【问题标题】:Applescript to click menu item of status bar appApplescript单击状态栏应用程序的菜单项
【发布时间】: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 命令确实会导致它打开,但是在菜单被关闭之前该命令不会完成,因此您无法发出后续命令来查找。使用trywith timeout 可能会在菜单仍然打开时重新获得控制权。
  • 如果这些提示、建议和评论对您没有任何好处——只是有不可编写脚本的应用程序——你可以看看一个绝对简单的 Foundation 应用程序/脚本(在下面),你可以使用(通过 osascript)单击任何 x/y 屏幕坐标,无论哪个应用程序负责此菜单(项)、按钮或窗口:apple.stackexchange.com/questions/316369/…

标签: macos applescript menubar


【解决方案1】:

使用包标识符而不是应用名称:

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

【讨论】:

  • 也可以尝试使用 menu item 2 之类的东西,而不是使用名称或 return entire contents of menu 1 来找出里面的内容。
  • 如何找到包标识符名称? "BUNDLE_IDENTIFIER_HERE". Invalid index.
  • 我试过... 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".
  • 找到我使用的捆绑列表/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' /Applications/Fenêtre.app/Contents/Info.plist
  • 这个答案是正确的并且有效!您所要做的就是更改选项名称并在 /Applications/ 文件夹的 Info.plist 中找到捆绑标识符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 2011-01-07
  • 2016-10-17
  • 2021-11-25
  • 1970-01-01
相关资源
最近更新 更多