如何编写基于标签或标识符的按钮点击脚本

How to script a button click based on Label or Identifier

我正在用 AppleScript 编写一个脚本,该脚本以应用程序中的按钮按下结束。然而,问题在于该按钮没有属性标题。我有 运行 Accessibility Inspector 和 UI 浏览器,AXTitle 是

我可以通过它的编号来调用按钮,但是我团队中的每个人都根据他们自己的工作流程对这个应用程序进行了稍微不同的设置,并且愚蠢的按钮有不同的标识号,具体取决于 window 所在的位置. window 可以是分离的 pop-up,也可以附加到左侧或右侧的主应用程序 window。所有三个选项都会产生不同的索引地址。


附加到左侧时的按钮索引:

tell application "System Events" to tell process "Application Name"
    click button 8 of splitter group 1 of front window of application 
    process "Application Name" of application "System Events"
end tell




附加到右侧时的按钮索引:

tell application "System Events" to tell process "Application Name"
    click button 9 of splitter group 1 of front window of application 
    process "Application Name" of application "System Events"
end tell




window 分离并浮动时的按钮索引:

tell application "System Events" to tell process "Application Name"
    click button 8 of front window of application process "Application    
    Name" of application "System Events"
end tell




位置之间的常量是按钮标签和按钮标识符,但我无法让脚本编辑器根据标识符或准备好的响应识别按钮。我不知道这是不可能的还是我只是写错了代码。

当我尝试使用标签或标识符时出现错误

click button "Prepared Response" of front window

脚本错误 系统事件发生错误:无法获取进程 window 1 的按钮 "Prepared Response"。

click button "_NS:667" of front window

脚本错误 系统事件发生错误:无法获取进程 window 1 的按钮“_NS:667”"Application Name"。

click button with "Prepared Response" of front window

语法错误 应为“into”、变量名、class 名称、其他参数名或属性 但找到“””。

click button with label "Prepared Response" of front window

语法错误 应为“given”、“with”、“without”、其他参数名称等,但找到了“””。


仅有的两个限制是代码必须是 AppleScript - 而不是 Javascript - 并且我不能要求我的团队成员安装应用程序

这是一种可能适合您的方法。

AppleScript 能够更改 property 值并在脚本的每个 运行 上保存这些新的 property 值。但是,如果脚本在任何时候被重新编译,那些已更改的新 property 值将丢失并设置回其原始值。

在下面的代码中,我设置了变量 property launchCount : 0 并在几行下面设置了另一个变量 set launchCount to launchCount + 1 。所以每次脚本 运行s,property launchCount : 0 将从 0 变为 1 然后 2 然后 3 等等......

此方法的目的是,对于首次 运行 脚本的每台计算机,都会出现一个 choose from list 对话框,要求用户 select 他们的window 位置。该选择将存储在另一个变量 property windowIsLocated 中。接下来是设置一个带有条件子句的处理程序...例如:if windowIsLocated is this then do that else if windowIsLocated is that then do this

property launchCount : 0
property windowIsLocated : missing value

set launchCount to launchCount + 1

set theList to {"Window Attached To Left", "Window Attached To Right", "Window Is Floating"}

if launchCount is 1 then
    set windowLocation to choose from list theList ¬
        with title "Window Location" with prompt ¬
        "Please Choose The Location Of Your Window" OK button name ¬
        "OK" cancel button name ¬
        "Cancel" multiple selections allowed false ¬
        without empty selection allowed
    set windowIsLocated to item 1 of windowLocation
end if

clickTheButton() -- use this anywhere in your script to click the button 

to clickTheButton()
    if windowIsLocated is "Window Attached To Left" then
        tell application "Application Name"
            activate
            repeat until frontmost
                delay 0.1
            end repeat
        end tell
        tell application "System Events" to tell process "Application Name"
            click button 8 of splitter group 1 of front window
        end tell
    else if windowIsLocated is "Window Attached To Right" then
        tell application "Application Name"
            activate
            repeat until frontmost
                delay 0.1
            end repeat
        end tell
        tell application "System Events" to tell process "Application Name"
            click button 9 of splitter group 1 of front window
        end tell
    else if windowIsLocated is "Window Is Floating" then
        tell application "Application Name"
            activate
            repeat until frontmost
                delay 0.1
            end repeat
        end tell
        tell application "System Events" to tell process "Application Name"
            click button 8 of front window
        end tell
    end if
end clickTheButton

阅读了您 post 的所有评论后,我决定添加一个 .gif 演示使用 Automator.app 的 "Watch Me Do" 记录我在系统偏好设置中点击某些按钮,然后将这些操作复制到脚本编辑器并使用该代码的元素来识别按钮和 windows 等

我发现 get properties 对调试很有用:

tell application "System Events" to tell process "Application Name"
  get properties of every UI element of front window
end tell

您会得到一堆垃圾,而且几乎所有垃圾都可能 (missing value) 用于您尝试编写脚本的应用程序。但是有的会被填写,比如description。一旦你弄清楚了,你可以通过以下方式消除所有噪音:

tell application "System Events" to tell process "Application Name"
  get description of every UI element of front window
end tell

但是,UI 元素是嵌套的。例如,您不会获得工具栏按钮,您可能只会获得 "toolbar" 作为第二个元素的描述。不是很有用...

要获取工具栏按钮,您可以执行以下操作:

tell application "System Events" to tell process "Application Name"
  get description of every UI element of toolbar 1 of front window
end tell

从那里,您应该可以轻松找到所需的工具栏按钮。那就很简单了:

tell application "System Events" to tell process "Application Name"
  repeat with uiElement in UI elements of toolbar 1 of front window
    if description of uiElement is "Do The Thing" then
      click uiElement
    end if
  end repeat
end tell