AppleScript -- 使用击键返回或键代码 36命令时,无法选择“菜单按钮”弹出窗口中的菜单项

问题描述

我经历过堆栈溢出,这似乎是一个重复的问题,但这更像是对现有问题的扩展问题!

我面临一个非常奇怪的问题,在我的应用程序中,我们自定义了弹出按钮(从 NSPopupButton 继承)。 当我使用 Apple Script 访问弹出项时,脚本无法从弹出列表中选择任何值。

让我说得更清楚!

例如。 想象一下弹出按钮有“汽车、自行车、自行车、公共汽车”作为它的项目。现在,如果我使用 AppleScript 选择 Bike,然后单击 Enter,则不会反映该值。但是如果我手动选择“自行车”,它将反映在应用程序中。 这是我写的脚本。

click menu button "Car" of window 1 of application process "My Sample Application" of application "System Events"
        key code 125 //I am using these to navigate down the list
        delay 1
        key code 125 //I am using these to navigate down the list
        delay 1
        key code 125 //I am using these to navigate down the list
        delay 3
        key code 36 //This line simulated ENTER/Return key (or NOT?)

我也试过在 Apple Script 中运行这个命令

--set xxx to value of every button of menu button "Car" of window 1 of application process "LG Calibration Studio" of application "System Events"

这个命令应该向我显示弹出窗口中存在的所有项目,但不幸的是它返回一个空数组(或字典)。即使弹出窗口填充了值,我也不知道为什么返回空。

我是 Apple Script 的新手,对这个领域的了解不多。任何帮助表示赞赏。 提前致谢:)

解决方法

在使用 UI 脚本时,ui 对象之间的关系并不总是很清楚。我将使用 Apple 的 Pages(因为它是免费提供的——我使用一个未修改的“论文”文档)作为示例。这里有两个建议供您探索。如果它们有意义,您可以在自己的应用中尝试类似的操作。

UI 元素命令

在新脚本中...

tell application "Pages"
    -- activate -- Not necessary until you want to act upon the window
    tell application "System Events" to tell front window of application process "Pages"
        UI elements -- generate list of window's ui elements
        properties of splitter group 1 -- the first element in the list (branch 1)
        UI elements of splitter group 1 -- generate list of first branch
        -- entire contents
    end tell
end tell

命令 1 的部分结果

{splitter group 1 of window "Untitled" of application process "Pages" of application "System Events",button 1 of window "Untitled" of application process "Pages" of application "System Events",button 2 of window "Untitled" of application process "Pages" of application "System Events"}

命令 2 的部分结果

{minimum value:missing value,orientation:missing value,position:{40,22}}

命令 3 的部分结果

{scroll area 1 of splitter group 1 of window "Untitled" of application process "Pages" of application "System Events",static text "0 Comments & Changes" of splitter group 1 of window "Untitled" of application process "Pages" of application "System Events",button "Comment" of splitter group 1 of window "Untitled" of application process "Pages" of application "System Events"}

使用 ui elements,您可以浏览窗口中无数的界面对象。您还可以询问特定的属性,例如name of every pop up button。最后,您可以使用 entire contents 获取所有 ui 元素的列表。

无障碍检查员

使用应用程序辅助功能检查器,您可以检查窗口并发现其元素。它位于 XCode 的应用程序包内,也可以通过 XCode > Open Developer Tool 菜单访问。

它会随着 XCode 的版本而变化,所以没有任何必要过于详细(我使用的是 Xcode 9)但是一旦它启动,点击左上角的“所有进程”下拉按钮,然后它应该列出打开的应用程序;从此列表中选择页面。

需要注意的几件事:底部是元素的层次结构,可以帮助确定要对哪个对象进行操作。每行括号内的文本都引用了对象的类,但文本并不总是文字(例如,您的脚本应该使用“窗口”而不是“标准窗口”)。

如果您使用“指针”,然后在 Pages 中单击格式检查器底部的空白区域,层次结构将在(滚动区域)下显示一长串项目。此外,与界面对象交互的方式有多种,“点击”只是其中的一种。

如果我将此代码插入脚本然后选择正文文本,脚本会将选择的格式设置为斜体(与层次结构条目“常规(弹出按钮)”相匹配,“常规”是选择的现有字体格式。

tell pop up button 2 of scroll area 2 of splitter group 1 of ¬
    window "Untitled" of application process "Pages" of ¬
    application "System Events" to perform action "AXPress"
delay 0.5
keystroke "i"
key code 36

希望在 UI 脚本编写头痛开始之前,利用这两种方法可能会产生一些有用的结果。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...