Applescript 如何基于标签或标识符为按钮单击编写脚本

Applescript 如何基于标签或标识符为按钮单击编写脚本,applescript,Applescript,我正在用AppleScript编写一个脚本,它以应用程序中的按钮按下结束。但是,问题是按钮没有属性标题。我已经在它上面运行了辅助功能检查器和UI浏览器,AXTitle是 我可以通过按钮的号码来调用该按钮,但是我团队中的每个人都根据自己的工作流程对该应用程序进行了稍微不同的设置,并且愚蠢的按钮具有不同的标识号,具体取决于窗口所在的位置。该窗口可以是分离的弹出窗口,也可以连接到左侧或右侧的主应用程序窗口。这三个选项都会产生不同的索引地址。 连接到左侧时的按钮索引: tell application

我正在用AppleScript编写一个脚本,它以应用程序中的按钮按下结束。但是,问题是按钮没有属性标题。我已经在它上面运行了辅助功能检查器和UI浏览器,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的“准备响应”按钮

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

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

语法错误应为“给定”、“有”、“无”、其他参数名称等,但找到了“”。



唯一的两个限制是代码必须是AppleScript,而不是Javascript,并且我不能要求我的团队成员安装应用程序。这里有一种方法可能适合您

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

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

这种方法的目的是,对于第一次运行脚本的每台计算机,会出现一个
从列表中选择
对话框,要求用户选择其窗口位置。然后,该选项将存储在另一个变量
属性窗口islocated
中。接下来是使用条件子句设置处理程序。。。例如:
如果windowIsLocated是这个,那么做那个;否则如果windowIsLocated是那个,那么做这个

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”记录我单击系统首选项中的一些按钮,然后将这些操作复制到脚本编辑器,并使用该代码的元素识别按钮和窗口等


以下是一种可能适合您的方法

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

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

这种方法的目的是,对于第一次运行脚本的每台计算机,会出现一个
从列表中选择
对话框,要求用户选择其窗口位置。然后,该选项将存储在另一个变量
属性窗口islocated
中。接下来是使用条件子句设置处理程序。。。例如:
如果windowIsLocated是这个,那么做那个;否则如果windowIsLocated是那个,那么做这个

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”记录我单击系统首选项中的一些按钮,然后将这些操作复制到脚本编辑器,并使用该代码的元素识别按钮和窗口等


我发现
获取属性对调试非常有用:

tell application "System Events" to tell process "Application Name"
  get properties of every UI element of front window
end tell
你会收到一堆垃圾,几乎所有的垃圾都可能是你试图编写脚本的应用程序的
(缺少值)
。但其中一些将被填写,例如
description
。一旦你明白了这一点,你就可以通过以下方式消除所有噪音:

tell application "System Events" to tell process "Application Name"
  get description of every UI element of front window
end tell
但是,UI元素是嵌套的。例如,您不会得到工具栏按钮,您可能只会得到第二个元素的描述。不是很有用

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

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

我发现
get properties
对于调试非常有用:

tell application "System Events" to tell process "Application Name"
  get properties of every UI element of front window
end tell
你会收到一堆垃圾,几乎所有的垃圾都可能是你试图编写脚本的应用程序的
(缺少值)
。但其中一些将被填写,例如
description
。一旦你明白了这一点,你就可以通过以下方式消除所有噪音:

tell application "System Events" to tell process "Application Name"
  get description of every UI element of front window
end tell
但是,UI元素是嵌套的。例如,您不会得到工具栏按钮,您可能只会得到