首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AppleScript --使用击键return或(键代码36)命令时,菜单按钮弹出菜单中的菜单项不可选

AppleScript --使用击键return或(键代码36)命令时,菜单按钮弹出菜单中的菜单项不可选
EN

Stack Overflow用户
提问于 2021-05-18 00:05:54
回答 1查看 155关注 0票数 0

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

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

让我说得更清楚一些!

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

代码语言:javascript
运行
复制
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中运行此命令

代码语言:javascript
运行
复制
--set xxx to value of every button of menu button "Car" of window 1 of application process "LG Calibration Studio" of application "System Events"

此命令本应显示弹出窗口中的所有项,但不幸的是,它返回了一个空数组(或字典)。即使弹出窗口中填充了值,我也不确定为什么返回为空。

我是苹果脚本的新手,在这个领域没有太多的知识。任何帮助都是非常感谢的。提前感谢:)

EN

回答 1

Stack Overflow用户

发布于 2021-05-18 06:25:23

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

UI元素命令

在新的脚本…中

代码语言:javascript
运行
复制
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的部分结果

代码语言:javascript
运行
复制
{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的部分结果

代码语言:javascript
运行
复制
{minimum value:missing value, orientation:missing value, position:{40, 22}}

命令3的部分结果

代码语言:javascript
运行
复制
{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版本的不同而不同,所以没有必要太详细(我使用的是Xcode9),但是一旦它启动,点击左上角的“所有进程”下拉按钮,它应该会列出打开的应用程序;从这个列表中选择页面。

有几件事需要注意:在底部是元素的层次结构,它可以帮助确定要对哪个对象执行操作。每一行的括号文本引用object类,但文本并不总是文字(例如,您的脚本应该使用'window‘而不是'standard window')。

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

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

代码语言:javascript
运行
复制
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脚本编写令人头疼之前产生一些有用的结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67573300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档