Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all windows of all applications

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?

Here's my code:

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

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

like image 974
inspectorG4dget Avatar asked Jan 27 '13 19:01

inspectorG4dget


2 Answers

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
like image 199
regulus6633 Avatar answered Oct 24 '22 03:10

regulus6633


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.

like image 30
Raymond Uppier-Püpschrute Avatar answered Oct 24 '22 04:10

Raymond Uppier-Püpschrute