在 Applescript 中,如何确定菜单项是否被选中/聚焦? [英] In Applescript, how can I find out if a menu item is selected/focused?

查看:71
本文介绍了在 Applescript 中,如何确定菜单项是否被选中/聚焦?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a script for OS X 10.5 that focuses the Search box in the Help menu of any application. I have it on a key combination and, much like Spotlight, I want it to toggle when I run the script. So, I want to detect if the search box is already focused for typing, and if so, type Esc instead of clicking the Help menu.

这是现在的脚本:

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

我正在考虑这样的事情:

And I'm thinking of something like this:

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

...但我收到此错误:

... but I get this error:

应用程序系统事件"的应用进程脚本编辑器"的菜单栏项目帮助"的菜单栏项目帮助"的{菜单项目1的帮助"菜单项1无法获得焦点}.

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"}.

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

So is there a way I can get my script to detect whether the search box is already focused?

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

I solved my problem by working around it. I still don't know how to check if a menu item is selected though, so I will leave this topic open.

推荐答案

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

Using /Developer/Applications/Utilities/Accessibility Tools/Accessibility Inspector.app you can use the built-in accessibility system to look at properties of the UI element under the mouse. Take special note of the cmd-F7 action to lock focus on an element and the Refresh button. Sadly the element and property names don't directly match those in the script suite, but you can look at the dictionary for System Events or usually guess the right terminology.

使用它您可以确定两件事.首先,focused 属性不在 menu item 上,而是 menu item 中有一个 text field代码> 重点.其次,菜单项有一个 selected 属性.

Using this you can determine two things. First, the focused property isn't on the menu item, but rather there is a text field within the menu item that is focused. Second, the menu item has a selected property.

有了这个,我想出了:

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 属性可能仍然存在问题.

Though this still doesn't work. The key event isn't firing as far as I can tell, so something may still be hinky with the focused property on the text field.

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

Anyway, your click again solution seems much easier.

这篇关于在 Applescript 中,如何确定菜单项是否被选中/聚焦?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆