[python-win32] Checking app windows for certain keywords

Tim Roberts timr at probo.com
Wed Dec 1 22:56:20 CET 2010


Kevin Carlson wrote:
> I'd like to develop a Python app that periodically scans all open app
> windows (browser, Word, Outlook, etc.) for specific keywords.

What does that mean, exactly?  Remember that most applications have
hundreds of windows (buttons, list boxes, input boxes, tree controls,
icons, menu bars, tool bars, etc).  Do you mean you want to scan their
open documents?  If you look at windows, you will often only get the
part that's visible right now.

> The idea is to have an alert window popup (using wxPython) as a reminder
> to the user to handle certain documents in a special way.

Hmm, I can see that getting really annoying, really quickly.

> Libraries such as WinGuiAuto.py can apparently check the contents of
> forms, but I haven't seen anything to simply identify keyword text
> anywhere within an open window.  Ideas?

I don't see why you think a form is different from a window. 
WinGuiAuto.py would seem to be just what you want, mostly.

However, it turns out that what you ask is simply not possible in the
general case.  You can certainly enumerate all of the top level windows
in the system, then enumerate all of the child windows looking for text
boxes and rich text controls, then fetch their contents (all using
WinGuiAuto.py).  Then you can search the text for your keywords. 
However, that won't work with Word (or any of the other Office
applications).  The Office applications do not use traditional windows
to display their controls.  They treat the entire window as a blank
canvas, and use their own internal scheme to display the text.  You
can't use a window message to read the visible part of a Word document.

To control Word, you'd have to use COM.  That opens up an entirely new
can of worms.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the python-win32 mailing list