Interacting with other Windoze apps

Jeff Shannon jeff at ccvcorp.com
Mon Apr 22 18:23:42 EDT 2002


In article <mailman.1019506545.30012.python-list at python.org>, 
brad-g-johnson at uiowa.edu says...
> Hello,
> 
> I'm a new Python programmer and have a question about "interacting" with
> other Windoze applications.  I'd like to send key strokes/characters to an
> application window, but have not been able to find any documentation on
> this.  Is it possible?  For example, suppose a user has Microsoft Word open.
> Is it possible to send an "Alt + F" then an "O" to the Word window to
> activate the *F*ile menu and select *O*pen?

This is not possible directly through Python.  However, using the 
Win32 extensions, it is possible to get access to the Windows 
Scripting Host (WSH), which includes (among many other things) 
the ability to activate another application, and to send 
keystrokes to the currently active application.  In order to do 
this, you'll need to find references for the WSH, most of which 
will probably be aimed at Visual Basic users -- WSH can be driven 
by any language with an ActiveScripting interface, but MS 
(unsurprisingly) focuses mostly on its own languages, VB and (to 
a much lesser extent) JScript/ECMAScript.  You'll also need to 
read up a bit on Python's COM framework (part of the Win32 
extensions), through which you can access the WSH.

Another alternative, which may or may not be easier, is to 
directly control applications that expose a COM automation 
interface (which many programs, including the major MS Office 
apps, do).  Thus, instead of asking Windows to send keystrokes to 
Word that result in  _F_ile/Open being selected, you can directly 
send Word a request to load a particular file.  You'll need to 
determine the object model for each application individually -- 
MS typically includes fairly good documentation for this within 
each application, but again, it focuses primarily on Visual 
Basic, and you'll have to translate to Python yourself.  If 
you're only interested in controlling a small number of different 
programs, this is probably the most effective way to go, but if 
you need to control arbitrary applications (or an app which 
specifically does not expose a COM interface) then you'll have to 
use the WSH as an intermediary.
 
-- 

Jeff Shannon
Technician/Programmer
Credit International



More information about the Python-list mailing list