12

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

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
  • 1
    Unless the app you're querying has explicit support for addressing each of it's windows (which is the case in most browser apps like Firefox and Safari, but likely not the case in most other apps), I suspect you need to have some aspect of the "Accessibility" preference pane turned on in order to get a handle to any / all windows in other apps. – Michael Dautermann Jan 27 '13 at 21:03
  • @MichaelDautermann: would you be able to point me to the exact value that I have to change and in which prefpane? – inspectorG4dget Jan 28 '13 at 00:20

2 Answers2

7

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
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • This is definitely an improvement over my code. However, it does not recognize any Google Chrome, Terminal or Preview windows. How would I go about fixing that problem? – inspectorG4dget Jan 27 '13 at 23:19
  • 1
    That's a tough problem to fix. Some applications may have documents instead of windows. Some may be directly scriptable, for example Terminal, where you can get the windows directly from the application. Some you may not find any solution at all. Just search, look at applescript dictionaries, and maybe you can find other solutions. I do not know of a direct answer for you. – regulus6633 Jan 28 '13 at 11:23
  • I have little to no knowledge of AppleScript, I am just interested in this problem specifically - just wanted to say that in my script editor it throws an error code of 1700 on `if not background only of theProcess then`, cannot convert <> of <> to Boolean – Scott Anderson Dec 20 '18 at 21:23
4

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.

  • this only lists the apps in the current desktop, is there a way to include all desktops? – bubakazouba Jan 20 '19 at 01:32
  • 1
    I got an error: error "System Events got an error: Can’t get attribute 1 of item 1 of every application process whose visible = true whose name = \"AXWindows\". Invalid index." number -1719 – Tyler Liu Sep 20 '21 at 21:55