列出所有应用程序的所有窗口

时间:2013-01-27 19:42:39

标签: macos window applescript

我正在尝试编写一个可以调整所有打开窗口大小的AppleScript脚本。为了确保我到达所有窗口,我正在让我的脚本说出应用程序的名称以及该应用程序的打开窗口数量。
有趣的是,当我听到所有打开的应用程序的名称时,我的脚本说他们都打开了0个窗口。我该如何解决这个问题?

这是我的代码:

tell application "System Events"
    repeat with theProcess in (every process)
        if background only of theProcess is false then
            if name of theProcess is not "Finder" then
                if name of theProcess is "Google Chrome" then
                    say "Chrome woo hoo"
                    say (count windows as string)
                else
                    say name of theProcess as string
                    say (count windows as string)
                    tell theProcess
                        repeat with theWindow in windows
                            say "found a window of"
                            say (name of theProcess) as string
                            tell theWindow
                                click button 2
                            end tell
                        end repeat
                    end tell
                end if
            end if
        end if
    end repeat
end tell

我使用的是Mac OS X 10.7.5,使用automator 2.2.4编写/运行此AppleScript

2 个答案:

答案 0 :(得分:6)

你必须告诉进程计算窗口。毕竟,这是了解其窗口而非系统事件的过程。

你告诉过程说出它的名字,例如“将theProcess的名称称为字符串”但是你只使用“说(将窗口计为字符串)”......没有任何过程与此相关联。尝试“计算过程的窗口”。基本上,你有时会告诉流程,有时你不会告诉流程,有时你告诉流程,即使你已经告诉过程,所以你要做两次。这就是你说“说(进程的名称)为字符串”,但该代码位于“告诉进程”块中,因此它已被告知进程。

你真的需要仔细阅读代码并更精确。提示...如果您想单击窗口中的按钮,则窗口必须位于屏幕的最前面,否则您无法单击它。另一个提示......“名称”已经是一个字符串,因此您不需要将其强制转换为字符串。

顺便说一句,我同意Michael Dautermann对你的帖子的评论......会有一些你无法访问的过程。但随着你的进步,你会发现它。

以下是我编写代码的方法。基本上我会在开头使用“tell theProcess”块获取所有变量。然后我可以用这些变量做些事情。我希望有所帮助。请注意,我只是将过程放在最前面,这意味着如果打开多个窗口,它只会单击前窗上的按钮。在单击其按钮之前,您必须添加代码以使每个窗口都显示在前面。祝你好运。

tell application "System Events"
    repeat with theProcess in processes
        if not background only of theProcess then
            tell theProcess
                set processName to name
                set theWindows to windows
            end tell
            set windowsCount to count of theWindows

            if processName is "Google Chrome" then
                say "Chrome woo hoo"
                say windowsCount as text
            else if processName is not "Finder" then
                say processName
                say windowsCount as text
                if windowsCount is greater than 0 then
                    repeat with theWindow in theWindows
                        say "found a window of " & processName
                        tell theProcess
                            set frontmost to true
                            tell theWindow
                                click button 2
                            end tell
                        end tell
                    end repeat
                end if
            end if
        end if
    end repeat
end tell

答案 1 :(得分:2)

我在Mavericks上创建了一个可见应用程序的所有打开窗口的列表,如下所示:

tell application "System Events"
    set this_info to {}
    repeat with theProcess in (application processes where visible is true)
        set this_info to this_info & (value of (first attribute whose name is "AXWindows") of theProcess)   
    end repeat
    this_info -- display list in results window of AppleScript Editor 
end tell

您需要允许任何使用此功能的应用访问“辅助功能”下的界面。

相关问题