如何根据标签或标识符来编写按钮单击的脚本

时间:2019-05-17 14:17:41

标签: applescript

我正在用AppleScript编写脚本,最后在应用程序中按下按钮。但是,问题在于该按钮没有属性标题。我已经在其上运行了Accessibility Inspector和UI Browser,并且AXTitle是

我可以通过按钮的编号来调用该按钮,但是团队中的每个人都根据自己的工作流程对此应用程序进行了稍微不同的设置,并且根据窗口的位置,愚蠢的按钮具有不同的标识号。该窗口可以是一个独立的弹出窗口,也可以连接到左侧或右侧的主应用程序窗口。这三个选项均产生不同的索引地址。


左侧附有按钮索引:

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




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

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

脚本错误系统事件发生错误:无法获取进程“应用程序名称”的窗口1的按钮“准备好的响应”。

click button "_NS:667" of front window

脚本错误系统事件发生错误:无法获取进程“应用程序名称”的窗口1的按钮“ _NS:667”。

click button with "Prepared Response" of front window

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

click button with label "Prepared Response" of front window

语法错误预期为“给出”,“有”,“没有”,其他参数名称等,但找到了“”。


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

2 个答案:

答案 0 :(得分:0)

这是一种可能对您有用的方法。

AppleScript能够更改property值并将这些新的property值保存在脚本的每次运行中。但是,如果脚本在任何时候都可以重新编译,则那些已更改的新property值将丢失并恢复为原始值。

在下面的代码中,我设置了变量property launchCount : 0,在接下来的几行中,我设置了另一个变量set launchCount to launchCount + 1。因此,每次脚本运行时,property launchCount : 0将从0变为1,然后从2变为3,依此类推...

此方法的目的是,对于每台首次运行脚本的个人计算机,都会出现一个choose from list对话框,要求用户选择其窗口位置。然后,该选择将存储在另一个变量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

在阅读您的帖子的所有评论后,我决定添加一个.gif文件,以使用Automator.app的“ Watch Me Do”进行演示,以记录我单击“系统偏好设置”中的某些按钮,然后将这些操作复制到“脚本编辑器”并该代码的元素来标识按钮和窗口等。

enter image description here

答案 1 :(得分:0)

我发现 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