pyatom/pyatom — pull request #79 by pehcha (2014-05-28 05:59:55+00:00)
Title: Fixed: generatemouseevent fails on mavericks

Hey guys,

The problem I encountered with **LDTP** is described [here](http://ldtp.myfastforum.org/ftopic90-0-0-asc-.php) (it was me who started the thread). And after some time of investigating I came to these results.

As far as I understand **LDTP** internally relies on the current frontmost application in OS while performing some basic API calls (e.g. `generatemousevent()`, `click()`, etc). It became obvious if you look at the [`generatemousevent` implementation](https://github.com/pyatom/pyatom/blob/master/atomac/ldtpd/mouse.py#L88).

But for some reason on some platform (OS X 10.9.3 in my case) [`getFrontmostApp`](https://github.com/pyatom/pyatom/blob/master/atomac/AXClasses.py#L93) returns an application that has no windows at all (e.g. in my case it returned [`loginwindow`](http://www.westwind.com/reference/OS-X/background-processes.html) process which has no windows while user is logged in).

And it was causing [`IndexError` exception while **LDTP** tried to fetch the first item in a process window list](https://github.com/pyatom/pyatom/blob/master/atomac/ldtpd/utils.py#L191).

To fix that I’ve added another function that returns not frontmost app but the first app which surely has some windows (look at `getAnyAppWithWindow` in `atomac/AXClasses.py`). And now every time **LDTP** tries to get the frontmost app it has one more chance to get another windowed application before it falls back to raising an error.

Also if you look my `_grabfocus` function in `atomac/ldtpd/utils.py` you’ll notice that I’ve added another way of processing of the `handle` because in my case it always was a tuple and actual handle was its first item.