关于 macos:在 Applescript 中,如何确定菜单项是否被选中/聚焦?

关于 macos:在 Applescript 中,如何确定菜单项是否被选中/聚焦?

In Applescript, how can I find out if a menu item is selected/focused?

我有一个用于 OS X 10.5 的脚本,它将搜索框集中在任何应用程序的帮助菜单中。我把它放在一个组合键上,就像 Spotlight 一样,我希望它在我运行脚本时切换。所以,我想检测搜索框是否已经专注于输入,如果是,请输入 Esc 而不是单击帮助菜单。

这是现在的脚本:

1
2
3
4
5
6
tell application"System Events"
    tell (first process whose frontmost is true)
        set helpMenuItem to menu bar item"Help" of menu bar 1
        click helpMenuItem
    end tell
end tell

我正在考虑这样的事情:

1
2
3
4
5
6
7
8
9
10
11
tell application"System Events"
    tell (first process whose frontmost is true)
        set helpMenuItem to menu bar item"Help" of menu bar 1
        set searchBox to menu item 1 of menu of helpMenuItem
        if (searchBox's focused) = true then
            key code 53 -- type esc
        else
            click helpMenuItem
        end if
    end tell
end tell

...但我收到此错误:

Can’t get focused of {menu item 1 of menu"Help" of menu bar item"Help" of menu bar 1 of application process"Script Editor" of application"System Events"}.

那么有没有办法让我的脚本检测搜索框是否已经聚焦?

我解决了我的问题。我仍然不知道如何检查菜单项是否被选中,所以我将保持这个话题打开。


你需要使用属性AXMenuItemMarkChar.

示例:

1
2
3
4
5
tell application"System Events"
    tell process"Cisco Jabber"
        set X to (value of attribute"AXMenuItemMarkChar" of menu item"Available" of menu"Status" of menu item"Status" of menu"File" of menu bar item"File" of menu bar 1) is"?" -- check if Status is"Availible"    
    end tell
end tell

如果选中菜单项,则返回值为?,否则为missing value

注意:此测试仅在其菜单被检查的应用程序当前位于最前面时才有效。


内置的快捷键 Cmd-? (Cmd-Shift-/) 已经像这样了。如果尚未获得焦点,它将键焦点移至帮助菜单的搜索字段,否则将关闭菜单。


我刚刚发现需要自己在 Illustrator 中进行一些文件处理。

这是我想出的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
tell application"Adobe Illustrator"
activate
tell application"System Events"
    tell process"Illustrator"
        set frontmost to true
        set activeMenuItem to enabled of menu item"Unlock All" of menu"Object" of menu bar item"Object" of menu bar 1
        if activeMenuItem is true then
            tell me to beep 3
        else
            tell me to beep 2
        end if
    end tell
end tell
end tell

完成。

这没有问题,可用于迭代文件。在我未来的自动化中,我可能不得不多次这样做。

祝你好运!


使用 /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app,您可以使用内置的辅助功能系统查看鼠标下 UI 元素的属性。请特别注意 cmd-F7 操作以将焦点锁定在元素和刷新按钮上。遗憾的是元素和属性名称与脚本套件中的名称不直接匹配,但您可以查看系统事件的字典或通常猜测正确的术语。

使用它可以确定两件事。首先,focused 属性不在 menu item 上,而是在 menu item 内有一个 text field 被聚焦。其次,菜单项有一个 selected 属性。

有了这个,我想出了:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
tell application"System Events"
    tell (first process whose frontmost is true)
        set helpMenuItem to menu bar item"Help" of menu bar 1

        -- Use reference form to avoid building intermediate object specifiers, which Accessibility apparently isn't good at resolving after the fact.
        set searchBox to a reference to menu item 1 of menu of helpMenuItem
        set searchField to a reference to text field 1 of searchBox

        if searchField's focused is true then
            key code 53 -- type esc
        else
            click helpMenuItem
        end if
    end tell
end tell

虽然这仍然行不通。据我所知,关键事件没有触发,因此文本字段上的 focused 属性可能仍然存在问题。

无论如何,您的 click 再次解决方案似乎要容易得多。


这对我来说可以使用 "selected" 属性在两个菜单项之间切换,根据选择的一个菜单项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
tell application"System Preferences"
    reveal anchor"keyboardTab" of pane"com.apple.preference.keyboard"
end tell
tell application"System Events" to tell process"System Preferences"
    tell pop up button 2 of tab group 1 of window 1
        click
        delay 0.2
        set appControl to menu item"App Controls" of menu 1
        set fKeys to menu item"F1, F2, etc. Keys" of menu 1
        if selected of appControl is true then
            click fKeys
        else
            click appControl
        end if
    end tell
end tell

推荐阅读

    清空linux命令搜索?

    清空linux命令搜索?,软件,系统,情况,命令,增长,工具,号码,密码,名称,位置,如

    linux脚本多个命令?

    linux脚本多个命令?,系统,工作,标准,服务,命令,软件,管理,连续,电脑,设备,lin

    linux脚本注释命令?

    linux脚本注释命令?,代码,系统,工作,名称,地方,脚本,命令,信息,状态,简介,lin

    脚本运行linux命令?

    脚本运行linux命令?,系统,环境,工具,工作,位置,底部,代码,发行,官网,终端,lin

    linux命令下写脚本?

    linux命令下写脚本?,服务,系统,环境,代码,名称,命令,脚本,发行,在线,情况,三

    linux把命令变成脚本?

    linux把命令变成脚本?,系统,环境,工作,脚本,命令,信息,基础,代码,文件,变量,

    脚本调用linux命令?

    脚本调用linux命令?,代码,系统,工作,底部,脚本,位置,环境,行用,官网,标准,typ

    linux脚本命令输信息?

    linux脚本命令输信息?,系统,信息,代码,脚本,数据,工具,发行,命令,变量,文件,l

    linux上启动脚本命令?

    linux上启动脚本命令?,服务,状态,系统,代码,脚本,工作,周期性,命令,文件,方

    linux命令搜索大全?

    linux命令搜索大全?,地址,工作,系统,命令,管理,目录,标准,基础,工具,网络,lin

    linux搜索过去命令?

    linux搜索过去命令?,系统,信息,命令,名称,工作,预期,数字,地址,标准,服务,Lin

    linux下编写脚本命令?

    linux下编写脚本命令?,代码,时间,工具,标准,系统,实战,平台,最新,网站,文件,l

    linux命令搜索命令?

    linux命令搜索命令?,系统,位置,地址,标准,管理,信息,命令,名称,工作,文件,Lin

    linux定时器脚本命令?

    linux定时器脚本命令?,系统,代码,时间,工具,工作,定期,周期性,异常,任务,脚

    linux停止脚本命令行?

    linux停止脚本命令行?,系统,工作,平台,命令,基础,第一,环境,信息,终端,程序,

    linux日志搜索命令?

    linux日志搜索命令?,信息,系统,对比,工具,一致,日志,文件,命令,实时,网络,lin

    linux命令字符搜索?

    linux命令字符搜索?,系统,工具,命令,灵活,信息,工作,字符串,文本,文件,模式,l

    linux命令运行脚本?

    linux命令运行脚本?,代码,系统,工作,设计,状态,命令,脚本,文件,目录,终端,运

    linux脚本赋权限命令?

    linux脚本赋权限命令?,系统,档案,工作,命令,工具,脚本,权限,文件,程序,文件

    linux关闭脚本命令?

    linux关闭脚本命令?,系统,服务,状态,命令,档案,暂停,软件,工具,数据,电脑,lin