【问题标题】:How do you programmatically press a toolbar button with AppleScript?您如何使用 AppleScript 以编程方式按下工具栏按钮?
【发布时间】:2010-06-11 11:19:10
【问题描述】:

我希望这不是太明显,但我想通过 AppleScript 按下应用程序中的工具栏按钮之一。

背景:该按钮没有任何菜单项或键盘快捷键。因此,我无法通过任何这些方法激活它;我需要找到一种实际“按下”按钮的 AppleScript 方式。

【问题讨论】:

    标签: macos button applescript toolbar


    【解决方案1】:

    我将稍微扩展一下@hced 所说的内容。

    我同意click button 3 of tool bar 1 of window 1 在许多情况下都有效。但是,从 OS 到 OS thic 可能是一个移动的目标。我的例子是 iTunes。在 iTunes 10.5.8 中,airplay 按钮是 17,而在 10.7.5 中是 25。然而,在这两种情况下,它都被称为“AirPlay”!

    所以我建议使用开发工具包,并从 Apple 获取以下代码: http://developer.apple.com/library/mac/#samplecode/UIElementInspector/Introduction/Intro.html

    运行该应用程序,无论您将鼠标悬停在何处,都会为您提供在 AppleScripts 中进行此项工作所需的所有信息:

    click (every button whose description is "AirPlay") of window "iTunes" of application process "iTunes"
    

    在这种情况下,应该只有一个带有此描述的按钮,但您可以使用该工具来发现是否是这种情况。如果是,请限制范围!

    希望这会有所帮助! 博

    【讨论】:

      【解决方案2】:

      啊,我自己也想出来了。看来我在这里大肆自我评论。

      这个例子对我有用,通过它的描述属性引用一个特定的工具栏按钮:

      tell application "System Events"
          tell process "OmniFocus"
              click (every button whose description is "Contexts") of tool bar 1 of window 1
          end tell
      end tell
      

      我将把它留给任何对引用描述为“上下文”的每个按钮有意见的评论者(根据我的示例),以表达下面的内容。但我认为不可能有多个按钮具有相同的描述,对吧?

      【讨论】:

        【解决方案3】:

        好吧,这比我想象的要简单,一旦我知道怎么做。 (谷歌搜索它并没有留下太多帮助,但是一个名为 Prefab UI Browser 的应用程序可以找出这是如何完成的)。

        例子:

        tell application "System Events"
            tell process "OmniFocus"
                click button 3 of tool bar 1 of window 1
            end tell
        end tell
        

        不过,我还有一个后续问题:

        如何按名称/描述引用工具栏按钮?我知道按钮有一个描述属性,其值为:“上下文”。但同样,如何在 click 语句中引用它?

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-05-07
          相关资源
          最近更新 更多