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"

这个命令本应显示Popup中的所有项,但不幸的是,它返回了一个空数组(或Dictionary(。尽管弹出窗口中填充了值,但我不确定为什么它会返回空值。

我是Apple Script的新手,在这个领域没有太多知识。感谢您的帮助。提前感谢:(

使用UI脚本时,UI对象之间的关系并不总是清晰的。我将使用苹果的页面(因为它是免费提供的——我使用一个未经修改的"随笔"文档(作为示例。这里有两个建议可以探讨。如果它们有意义,你可以用自己的应用程序尝试类似的操作。

"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>打开"开发人员工具"菜单。

它随着XCode的版本而变化,所以没有必要太详细(我在XCode 9上(,但一旦它启动,点击左上角的"所有进程"下拉按钮,它应该会列出打开的应用程序;从该列表中选择"页面"。

需要注意的几点是:底部是元素的层次结构,它可以帮助确定要对哪个对象执行操作。每一行的括号中的文本都引用了对象的类,但文本并不总是文本(例如,脚本应该使用"window"而不是"standard window"(。

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

如果我将此代码插入到脚本中,然后选择正文,脚本将把所选内容格式化为斜体(这与层次结构条目"Regular(弹出按钮("匹配,"Regular"是所选内容的现有字体格式。

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脚本头疼之前产生一些有用的结果。

最新更新