AppleScript 或 Automator 用于单击应用程序中的菜单?

发布于 2024-08-18 21:25:34 字数 500 浏览 4 评论 0原文

我不确定这是否可以通过 AppleScript 和/或 Automator 完成...但我希望能够:

a) 启动应用程序(我知道这可以通过 AppleScript 或 Automator 轻松完成)

b) 应用程序启动后,使用 AppleScript 或 Automator 选择特定的菜单项。

例如,我想启动 Excel 2008(我有家庭/学生版,但没有为 Automator 进行预配置),然后单击“文件”菜单并单击“打开”。

有没有关于去哪里/寻找如何选择这样的菜单项的指示(或者是否有可能)?

您可以使用 Automator 的 Record 功能“某种程度上”执行此操作,但 Record 非常脆弱。

我宁愿能够使用 AppleScript 简单地获取一个包含应用程序的每个菜单项的“数组”,然后以编程方式单击数组中的第 0 个菜单项……等等。等等,

这可能吗?

TIA

I'm not sure if this is do-able via AppleScript and/or Automator…but I'd like to be able to:

a) launch an application (I know this can be done pretty easily by AppleScript or Automator)

b) once the application is launched, use AppleScript or Automator to select specific menu items.

e.g. I'd like to launch Excel 2008 (I have the home/student edition which doesn't come preconfigured for Automator) and then click the "File" menu and click on "open".

Any pointers on where to go/look for how to select menu items like this (or if it's even possible at all)?

You can "sort of" do this using Automator's Record function, but Record is very brittle.

I'd rather be able to use AppleScript to simply grab an "array" that contains every menu item for the application and then programmatically click on the 0th menu item in my array…etc. etc.

Is this possible?

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我不是你的备胎 2024-08-25 21:25:34
tell application "###"

activate

end tell


tell application "System Events"

tell process "###"

click menu item "^^^" of menu "$$" of menu bar 1

end tell

end tell

将您的应用程序放入 ###,将您的菜单项放入 ^^^,并将您的菜单(文件、编辑、查看等)放入 $$$。大小写很重要。

将其放入 applescript 顺便说

一句示例:

tell application "iTunes"

activate

end tell


tell application "System Events"

tell process "iTunes"

click menu item "as list" of menu "view" of menu bar 1

end tell

end tell

删除 END TELL 和 TELL APPLICATION "SYSTEM EVENTS" 之间的双空格

tell application "###"

activate

end tell


tell application "System Events"

tell process "###"

click menu item "^^^" of menu "$$" of menu bar 1

end tell

end tell

Put your application in for the ### and put your menu item in for the ^^^ and put your menu (file, edit, view, etc.) in for $$$. Capitalization matters.

Put it in applescript btw

EXAMPLE:

tell application "iTunes"

activate

end tell


tell application "System Events"

tell process "iTunes"

click menu item "as list" of menu "view" of menu bar 1

end tell

end tell

delete the double spaces EXCEPT BETWEEN END TELL AND TELL APPLICATION "SYSTEM EVENTS"

翻身的咸鱼 2024-08-25 21:25:34

我经常需要创建一个 GUI 脚本来访问应用程序中的菜单项,而这些应用程序的 AppleScript 库不提供对菜单项所代表的对象或函数的直接访问。为了方便重用代码,我使用应用程序、菜单和应用程序的变量。菜单项名称。然后我只需要更改顶部的变量,而不是从代码主体中挑选名称。

set targetApp to "app_name"
set theMenu to "menu_name"
set theItem to "menu_item_name"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        click menu item theItem
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

子菜单

当有子菜单和子菜单时,它会变得更加复杂。涉及子子菜单,因为带有子菜单的菜单项既是其父菜单的菜单项,又是其子菜单的菜单父项。请注意,文本变量“theItem”用于指定菜单项和菜单; “targetApp”字符串变量用于引用应用程序和应用程序。一个过程,因此在重用代码时无需在 2 个位置分别编辑 2 个名称。我使用此脚本在语音命令上运行,以便快速访问菜单项,而不必说,例如“单击编辑菜单”...“单击转换”...“单击大写”...我添加另一个变量对于子菜单项:

set targetApp to "app_name"
set theMenu to "menu_name"
set theItem to "menu_item_name"
set theSubItem to "sub_item_name"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        tell menu item theItem
                            tell menu theItem
                                click menu item theSubItem
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

例如:

set targetApp to "TextEdit"
set theMenu to "Edit"
set theItem to "Transformations"
set theSubItem to "Make Upper Case"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        tell menu item theItem
                            tell menu theItem
                                click menu item theSubItem
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

如果有另一级子菜单,则需要附加变量(例如“theSubSubItem”),&系统事件进程告诉块中的行将有另一层
...

                            tell menu item theItem
                                tell menu theSubItem
                                    click menu item theSubSubItem
                                end tell
                            end tell

...
正如本线程中其他地方所述,建议解决应用程序的对象和对象。只要它们包含在 API 中,就可以直接使用它们,但是当 API 不提供直接访问时,寻址 GUI 作为最后的手段很有用。缺点是 GUI 脚本可能会变得更加麻烦且复杂。可能需要随着每次应用程序更新而进行修改。

I frequently need to create a GUI script to access a menu item in applications whose AppleScript library doesn't provide direct access to the objects or functions the menu item represents. To make it easy to re-use the code, I use variables for the app, menu, & menu item names. Then I just need to change the variables at the top rather than pick out the names from the code body.

set targetApp to "app_name"
set theMenu to "menu_name"
set theItem to "menu_item_name"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        click menu item theItem
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

SUB-MENUS

It gets a little more involved when there are sub- & sub-sub-menus involved, as a menu item with a sub-menu is both a menu item of its parent menu AND a menu parent of its sub-menus. Note that the text variable "theItem" is used to specify both a menu item AND a menu; the "targetApp" string variable is used to reference both an app & a process, so it saves having to edit 2 names in 2 places each when reusing the code. I use this script to run on voice commands for accessing menu items quickly rather than having to say, e.g., "click Edit Menu"... "click Transformations"... "click Make Upper Case"... I add another variable for the sub-menu item:

set targetApp to "app_name"
set theMenu to "menu_name"
set theItem to "menu_item_name"
set theSubItem to "sub_item_name"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        tell menu item theItem
                            tell menu theItem
                                click menu item theSubItem
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

For example:

set targetApp to "TextEdit"
set theMenu to "Edit"
set theItem to "Transformations"
set theSubItem to "Make Upper Case"

tell application targetApp
    activate
    tell application "System Events"
        tell application process targetApp
            tell menu bar 1
                tell menu bar item theMenu
                    tell menu theMenu
                        tell menu item theItem
                            tell menu theItem
                                click menu item theSubItem
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

If there's another level of sub-menus, an additional variable (e.g., "theSubSubItem") would be required, & the line in the System Events process tell block would have another layer
...

                            tell menu item theItem
                                tell menu theSubItem
                                    click menu item theSubSubItem
                                end tell
                            end tell

...
As noted elsewhere in this thread it's recommended to address an application's objects & functions directly whenever they're included in the API, but addressing the GUI is useful as a last resort when the API doesn't provide direct access. The downside is a GUI script can get more cumbersome & may have to be revised with each application update.

许仙没带伞 2024-08-25 21:25:34

事实上,UI 脚本是脆弱且挑剔的,但您可以查看 Apple 的 AppleScript GUI 脚本页面

Indeed, UI scripting is fragile and finicky, but you might take a look at Apple's AppleScript GUI Scripting page

南冥有猫 2024-08-25 21:25:34

UI 脚本是一个变化无常的野兽。它很脆弱,并且通常对许多您无法控制的事情敏感,例如意外的对话框(来自其他应用程序)、系统字体或语言的更改、用户活动。更不用说跨版本应用程序 UI 的变化了。

通过 AppleScript API 直接与应用程序对话要健壮得多。这正是 AppleScript 的目的。您始终可以通过 AppleScript Editor.app 中的“打开词典...”菜单项查看应用程序可以通过 AppleScript 提供的操作和数据(在文件对话框中选择要编写脚本的应用程序)。您无需模拟菜单上的鼠标单击,而是通过 AppleScript 直接调用菜单项触发的操作。

当然,并非所有应用程序都有完整的(甚至任何)AppleScript API。不过,如果您的目标是 Excel,那么它具有出色的 AppleScript 支持。

如果您确实尝试进行 UI 测试,我建议您从其他选项开始:

  1. 测试模型(类似于通过 AppleScript 编写应用程序模型的脚本)。事实上,如果您的应用程序中包含 AppleScript API,您也可以自动测试该 API。
  2. 使用 Google Toolbox for Mac 单元测试添加内容(或推出您自己的)以编程方式将鼠标或键盘事件发送到您的应用程序。
  3. 使用 Instruments.app 记录 UI 序列。 Instruments 的录音非常可靠(我没有使用 Automator 的录音功能),而且您还可以获得 Instruments 的所有其他优点。

UI scripting is a fickle beast. It's brittle and often sensitive to many things beyond your control such as unexpected dialogs (from other applications), changes in system font or language, user activity. Not to mention changes in an application's UI across versions.

It's much more robust to use talk directly to the application via its AppleScript API. This is exactly the purpose of AppleScript. You can always see the actions and data an application can provide via AppleScript via the "Open Dictionary..." menu item in AppleScript Editor.app's (select the application you want to script in the file dialog). Instead of simulating a mouse click on a menu, you call the action that menu item triggers directly via AppleScript.

Of course not all applications have a complete (or even any) AppleScript API. Excel, however, has excellent AppleScript support, if that's your target.

If you're really trying to do UI testing, I recommend you start with other options:

  1. Test the model (analogous to scripting the app's model via AppleScript). In fact, if you include an AppleScript API in your app, you can automated testing of that API as well.
  2. Use Google Toolbox for Mac's unit testing additions (or roll your own) to programatically send mouse or keyboard events to your app.
  3. Use Instruments.app to record a UI sequence. The Instruments recording is solid (I haven't used Automator's Record feature), and you get all the other goodies of Instruments too.
⊕婉儿 2024-08-25 21:25:34

我不知道如何抓取数组,但这看起来可能适用于发送鼠标点击等:

虚拟输入

I don't know about grabbing an array, but this looks like it might work for sending the mouse clicks, etc:

VirtualInput

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文