Listing all windows of all applications

后端 未结 2 854
说谎
说谎 2020-12-30 12:56

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 t

相关标签:
2条回答
  • 2020-12-30 13:32

    I create a list of all open windows of visible applications on Mavericks like this:

    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
    

    You need to allow any app using this to access the interface under Accessibility.

    0 讨论(0)
  • 2020-12-30 13:44

    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.

    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.

    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.

    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
    
    0 讨论(0)
提交回复
热议问题