Applescript 关闭包含静态文本的对话框

Applescript 关闭包含静态文本的对话框,applescript,Applescript,我正在尝试编写一个脚本,在包含特定静态文本的每个对话框上按OK按钮: tell application "System Events" set theProcesses to application processes repeat with theProcess from 1 to count theProcesses tell process theProcess repeat with x from 1 to count window

我正在尝试编写一个脚本,在包含特定静态文本的每个对话框上按OK按钮:

tell application "System Events"
    set theProcesses to application processes
    repeat with theProcess from 1 to count theProcesses
        tell process theProcess
            repeat with x from 1 to count windows
                set texts to static text of window x
                    repeat with t from 1 to (count texts)
                        if (static text t of window x whose value is "Particular text") then
                            click first button whose value is "OK" of window x
                        end if
                    end repeat
                end repeat
            end tell
    end repeat
end tell
此脚本未执行。if语句有问题。

您必须检查

if (value of static text t of window x is "Particular text")

如果“确定”按钮不存在,则有必要使用
try
块来忽略错误

tell application "System Events"
    set theProcesses to application processes
    repeat with theProcess from 1 to count theProcesses
        tell process theProcess
            repeat with x from 1 to count windows
                set texts to static text of window x
                repeat with t from 1 to (count texts)
                    if (value of static text t of window x is "Particular text") then
                        try
                            click (first button of window x whose value is "OK")
                        end try
                    end if
                end repeat
            end repeat
        end tell
    end repeat
end tell
你必须检查一下

if (value of static text t of window x is "Particular text")

如果“确定”按钮不存在,则有必要使用
try
块来忽略错误

tell application "System Events"
    set theProcesses to application processes
    repeat with theProcess from 1 to count theProcesses
        tell process theProcess
            repeat with x from 1 to count windows
                set texts to static text of window x
                repeat with t from 1 to (count texts)
                    if (value of static text t of window x is "Particular text") then
                        try
                            click (first button of window x whose value is "OK")
                        end try
                    end if
                end repeat
            end repeat
        end tell
    end repeat
end tell

作为一种替代方法,理论上,如果有大量的进程、窗口和文本对象要循环,那么可以更快地完成这项工作,即一次抓取所有内容。这可以在一行中完成,但将其分解为单独的行不会牺牲任何速度,并提供更易于阅读的代码块:

    tell application "System Events"
        set P to a reference to every process
        set W to a reference to every window of P
        set S to a reference to (static texts of W whose value is "Particular text")
        set C to a reference to value of attribute "AXParent" of S
        set B to a reference to (buttons of C whose name is "OK")

        repeat with _b in B
            click _b
        end repeat
    end tell
用一些粗略的解释将其分解:

p
:包含对每个流程的引用。因为它是对每个进程的引用,而不仅仅是进程本身,AppleScript不需要花费任何精力来确定这些进程是什么。它基本上将稍后执行此操作的指令存储在变量
P

W
:是对每个流程的每个窗口的引用。到目前为止,这两行相当于您的前两个
repeat
循环,只是到目前为止没有执行任何计算,而您的脚本将经历(
计数窗口
×
计数过程
计算)

S
:是对与所需值匹配的所有静态文本的引用。无论出于何种目的,这种方法都“绕过”了单独循环它们的需要,只需立即获取正确的循环(实际上,循环仍在执行,但它是在更深的代码级别执行的,这比AppleScript执行的任何循环所需的时间都要少)

C
:包含静态文本的父级,即包含所述文本的窗口。(
C
表示容器=父容器)

B
:最终是包含特定静态文本的每个窗口的所有“OK”按钮的集合。它是对它们的引用,因此,同样,可以一次抓住它们,避免
重复
循环

最后,要单击
B
中的按钮,我们将进行第一次真正的计算,并循环执行
B
。但是,
B
只包含需要单击的按钮,因此没有浪费的迭代,因此不需要
try…end try

由于
B
中的每个按钮都是通过变量
\u B
访问的,因此AppleScript将所有这些引用放在一起,并执行检索按钮所需的必要(且仅必要)计算,这相当于对每个
repeat
循环进行一次迭代

虽然这已经是获取需要单击的按钮的非常有效的方法,但我们可以过滤流程和窗口以进一步改进:

    tell application "System Events"
        set P to a reference to (processes whose background only is false)
        set W to a reference to (windows of P whose subrole is not "AXUnknown" and role is "AXWindow")
        set S to a reference to (static texts of W whose value is "Particular text")
        set C to a reference to value of attribute "AXParent" of S
        set B to a reference to (buttons of C whose name is "OK")

        repeat with _b in B
            click _b
        end repeat
    end tell
正如我提到的,额外的优点是,这需要更少的错误处理(运行早于MacOS 10.12的OS X版本的系统可能需要将
repeat
循环包装在
try
块中;在MacOS 10.12及更高版本中,这是不需要的)。如果窗口x的
第一个按钮的值为“OK”
返回空结果,则脚本中需要
try
块,这将导致
单击
抛出错误。此处,
B
仅包含名为“OK”的按钮(如果需要,将
名称
更改为
),因此
单击
将始终有一些内容可单击

然而,为了健壮性和满足早期系统的需要,在错误处理方面考虑周全可能是一种好的做法:

    try
        repeat with _b in B
            click _b
        end repeat
    end try
因此,值得一提的是,您的脚本可能需要额外的错误处理,而不仅仅是单个
try
块:单击“确定”按钮后,我猜对话框将消失。发生这种情况时,它包含的所有静态文本对象都会随之消失,这意味着不完整的
repeat with t
循环将引发另一个错误



感谢您在OS X 10.8.6中测试代码。

作为一种替代方法,理论上,如果您有大量的进程、窗口和文本对象要循环,则可以更快地完成这项工作,即一次抓取所有内容。这可以在一行中完成,但将其分解为单独的行不会牺牲任何速度,并提供更易于阅读的代码块:

    tell application "System Events"
        set P to a reference to every process
        set W to a reference to every window of P
        set S to a reference to (static texts of W whose value is "Particular text")
        set C to a reference to value of attribute "AXParent" of S
        set B to a reference to (buttons of C whose name is "OK")

        repeat with _b in B
            click _b
        end repeat
    end tell
用一些粗略的解释将其分解:

p
:包含对每个流程的引用。因为它是对每个进程的引用,而不仅仅是进程本身,AppleScript不需要花费任何精力来确定这些进程是什么。它基本上将稍后执行此操作的指令存储在变量
P

W
:是对每个流程的每个窗口的引用。到目前为止,这两行相当于您的前两个
repeat
循环,只是到目前为止没有执行任何计算,而您的脚本将经历(
计数窗口
×
计数过程
计算)

S
:是对与所需值匹配的所有静态文本的引用。无论出于何种目的,这种方法都“绕过”了单独循环它们的需要,只需立即获取正确的循环(实际上,循环仍在执行,但它是在更深的代码级别执行的,这比AppleScript执行的任何循环所需的时间都要少)