列出所有应用程序的所有窗口 [英] Listing all windows of all applications

查看:27
本文介绍了列出所有应用程序的所有窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to write an applescript script that resizes all open windows. In order to make sure that I'm getting to all the windows, I'm making my script say the name of the application as well as the number of open windows of that application.
Interestingly, while I hear the names of all my open applications, my script says that they all have 0 windows open. How can I fix this issue?

这是我的代码:

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

I'm on Mac OS X 10.7.5, using automator 2.2.4 to write/run this applescript

推荐答案

你必须告诉进程计数窗口.毕竟是进程知道它的窗口,而不是系统事件.

You have to tell the process to count windows. After all it's the process that knows about its windows, not system events.

您已经告诉进程说出它的名字,例如将进程的名称作为字符串说"但是您只使用说(将窗口计数为字符串)"......没有任何进程与此相关.尝试计算进程的窗口".基本上你有一些台词,有时你告诉过程,有时你不,有时你告诉过程,即使你已经告诉了过程,所以你要重复两次.这就是您将说(进程名称)作为字符串"的地方,但该代码位于告诉进程"块内,因此它已经被告知进程.

You have told the process to say its name e.g. "say name of theProcess as string" however you only use "say (count windows as string)"... no process is tied to that. Try "count windows of theProcess". Basically you have lines where sometimes you tell the process, other times you don't, and other times where you tell the process even though you've already told the process, so you do it twice. That's where you have "say (name of theProcess) as string" but that code is inside a "tell theProcess" block so it's already being told to theProcess.

实际上,您需要仔细检查代码并更加精确.提示...如果您想单击窗口中的按钮,则该窗口必须位于屏幕的最前面,否则您无法单击它.另一个提示...name"已经是一个字符串,因此您无需将其强制转换为字符串.

Really you need to go through your code and be more precise. A tip... if you want to click a button in a window then the window must be frontmost on the screen otherwise you can't click it. Another tip... "name" is already a string so you don't need to coerce that to a string.

顺便说一下,我同意 Michael Dautermann 对您的帖子的评论……有些流程您将无法访问.但随着你的进步,你会发现这一点.

By the way, I agree with Michael Dautermann's comment to your post... there will be processes where you won't get access. But you'll find that out as you progress.

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

Here's how I would write your code. Basically I would get all of the variables at the beginning using a "tell theProcess" block. Then I can do stuff with those variables. I hope that helps. Notice that I only made the process frontmost which means if it has multiple windows open it will only click a button on the front window. You'll have to add code to make each window come to the front before you can click its button. Good luck.

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

这篇关于列出所有应用程序的所有窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆