[HowTo] Get app window if it is not window(0) - shows region (0,0) 0x0 or similar nonsense

Created by RaiMan on on 2011-08-08
Keywords:
Last updated by:
RaiMan on on 2011-08-08

*** The problem
# supposing the app is already started
theApp = App("some application")
theApp.focus()
print theApp.window()

you expect window() to be the frontmost visible window, but this is not the case.

--------------------------------------------------------------------------------------------------------------

Especially on Windows, you can see, that an Application might have many windows, while only one of them is currently visible.

Just make this test with Firefox already started:

ffa = "firefox"
ff = App(ffa)
ff.focus()
for i in range(100):
    w = ff.window(i)
    if not w: break
    print i, w

there are some windows at (0,0) or even negative positions. Some of them have dimension 0x0, others are rather small.

If I doubt, that the currently visible app window is App.window() (being window(0) actually), I make the following filter loop:

ffa = "firefox"
ff = App(ffa)
ff.focus()
for i in range(100):
    w = ff.window(i)
    if not w: break
    if w.x <1 or w.y <1: continue
    if w.w <200 or w.h<200: continue # thresholds might vary
print i, w
w.highlight(2)

This usually leaves you with w as being the app window you are looking for. For one app (Firefox in this case) this is usually stable, so if you know, the apps frontmost visible window is window(1), you can leave the filtering and use directly:

ffa = "firefox"
ff = App(ffa)
ff.focus()
w = ff.window(1)

e.g. Chrome Version 13: the frontmost visible app window is window(5)