Expanding Python as a macro language

Alex Martelli aleaxit at yahoo.com
Sat Oct 29 12:21:33 EDT 2005


<qwweeeit at yahoo.it> wrote:
   ...
> But the problem is that in Linux you can't even send a keystroke to
> a running GUI application!

Actually, if the app is running under X11 you may try to fake out a
keystroke event (with low level calls, but ctypes might let you use it
from Python).  Of course, the app WILL be told that the keystroke is
fake, through a special flag if it cares to check for it, for security
reasons; but if the app doesn't specifically defend itself in this way.

See, for example, http://xmacro.sourceforge.net/ -- I guess that
xmacroplay could pretty easily be adapted, or maybe even used as is with
an os.popen.


> I want to find a solution in Linux, with the help of experts
> (if they don't use only Windows...)  for two reasons:
> - the reduced availability in Windows of "free" or "open" applications
> - the more severe security problems in Windows.
> Concerning the second point, you can correctly argue that this is,
> at least partly, due to the wider market share of Windows but IMHO
> Linux is more robust in this field, and ...at the present times the
> situation is like that!

Don't neglect MacOSX -- it's quite secure, there are many open and free
applications, AND it has a decent architecture for the kind of tasks you
want to do (mostly intended for Apple's own Applescript language, but
all the interfaces are open and easily available to Python, which is
quite well supported on the Mac).

It also has an excellent italian newsgroup, it.comp.macintosh -- quite
high volume (as it discusses ANYthing Apple, from iPod shuffles to
golden oldies to rumors about new servers &c, with a lot of volume on
the audio and video applications that macs do so well) but worth it.


However, all the specific use cases you describe are best handled by
either fully emulating or directly integrating with a browser; faking
keystrokes is definitely too low-level an approach.  Python is very good
at dealing with the web (it IS, apparently, the favourite language of
Tim Berners Lee --- he came give a keynote at a Python conference),
including recording and replaying cookies and anything else you may need
to make a "special purpose browser" for automation purposes. Twisted is
an asynchronous framework for very well-performing, lightweight clients
and servers -- or, Python's standard library can suffice if you're not
in a terrible hurry;-).

Alternatively, Firefox and other Mozilla Foundation apps are designed to
be automated via XPCOM, essentially a cross-platform equivalent of
Microsoft's good old COM, and there are Python interfaces to it (some
future Firefox version might perhaps integrate a Python engine, just
like it integrates a Javascript engine today, but I wouldn't hold my
breath waiting;-).


Alex



More information about the Python-list mailing list