【问题标题】:AppleScript -- Menu Item in the Menu Button popup is not selectable while using keystroke return or (key code 36) commandAppleScript - 使用按键返回或(键代码 36)命令时,菜单按钮弹出窗口中的菜单项不可选择
【发布时间】:2021-08-06 22:17:18
【问题描述】:

我经历了堆栈溢出,这看起来像是一个重复的问题,但这更像是对现有问题的扩展问题!

我面临一个非常奇怪的问题,在我的应用程序中,我们自定义了弹出按钮(继承自 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 的新手,在这个领域没有太多知识。任何帮助表示赞赏。 在此先感谢:)

【问题讨论】:

  • 当您在应用程序中创建按钮时,是否有特殊原因需要编写用户界面脚本?
  • 是的,只是帮助测试团队自动化一些“非常常见”的测试场景。我知道我们有 UITesting,但这个项目是测试团队开始的,而且他们大多无法访问我们的代码,而且他们也不熟悉 Xcode。
  • 这能回答你的问题吗? Selecting Pop Up Menu Buttons in AppleScript
  • 不,我已经试过了。我收到错误消息,告诉该属性没有退出。

标签: objective-c macos applescript popupmenu applescript-objc


【解决方案1】:

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

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 元素的列表。

辅助功能检查器

使用应用程序 Accessibility Inspector,您可以检查窗口并发现其元素。它位于 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 脚本头痛发作之前产生一些有用的结果。

【讨论】:

  • 提示:脚本 tell application "System Events" to click at {360, 190} 返回该位置的 UI 元素。从 Command-Shift-4 光标获取坐标。
猜你喜欢
  • 2016-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多