如何检查applescript中是否存在进程窗口?

如何检查applescript中是否存在进程窗口?,applescript,Applescript,我正在这样做: tell application "System Events" to click button "Reply" of window 1 of process "Notification Center" 如果没有窗口,会出现异常。所以我试了一下: if exists (window 1 of process "Notification Center") then tell application "System Events" to click button "Repl

我正在这样做:

tell application "System Events" to click button "Reply" of window 1 of process "Notification Center"
如果没有窗口,会出现异常。所以我试了一下:

if exists (window 1 of process "Notification Center") then
    tell application "System Events" to click button "Reply" of window 1 of process "Notification Center"
end if 

但在某种情况下,applescript似乎无法执行进程的
窗口1
。如何实现这一点?

首先,一个简单的测试表明,AppleScript可以在一定条件下完成进程的
窗口1。我使用以下方法对其进行了测试:

tell application "System Events"
    if exists (window 1 of process "Safari") then
        display dialog "Found window 1"
    end if
end tell
如果Safari中有任何打开的窗口,它将显示对话框窗口,否则不会显示。因此,问题在别处

事实上,当我键入此内容时,出现了一个通知,让我知道我可以参观“OS X Yosemite的新功能”,因此我借此机会运行:

tell application "System Events"
    if exists (window 1 of process "Notification Center") then
        display dialog "Found window 1"
    end if
end tell
它实际上显示了对话框;在我按下通知上的“关闭”按钮后,相同的脚本没有检测到窗口,然后显示对话框

下面是一个脚本,它将创建自己的通知窗口,然后尝试检测它:

display notification "Hello"

--without a delay, this script will not immediately notice the window
delay 1

tell application "System Events"
    if exists (window 1 of process "Notification Center") then
        display dialog "Found window 1"
    end if
end tell
如果您删除或注释掉
delay 1
行,在我写这篇文章的有限测试中,它不会检测到窗口存在

这让我怀疑您尝试与之交谈的流程不是通知中心,或者,更可能的是,您尝试与它交谈的速度过快

如果您自己创建通知,或者在通知显示后立即响应通知,则可能需要延迟才能检测到通知。(如果预期通知窗口出现与实际出现之间的时间不确定,则可能需要将延迟和条件放入循环。)


当你说“AppleScript不行”时,你是什么意思?它是在那一行给出了错误,还是只是没有检测到窗口?

是的,它给出了一个错误:
预期,但找到了“”
。顺便说一句,我是小牛队的。啊,如果我把我的情况放在那个告诉块(或者不管他们叫什么)里面,效果很好。啊,是的,他们叫告诉块。它们表示哪个应用程序应该回答您提出的问题;在这种情况下,System Events是理解进程是什么的应用程序,因此我们告诉System Events回答我们关于Notification Center进程中存在哪些窗口的问题。但是我们如何确保在延迟1秒后脚本将检测到该窗口的存在。即使我在某些机器上也面临同样的问题,它需要2秒的延迟。是否有其他方法可以毫不延迟地轮询窗口是否存在