tags:

views:

1004

answers:

3

Is it possible in MacOS X to send a mouse click to a specific window?

I already managed to send a click to the entire screen via CGPostMouseEvent. The windows i want to control overlap each other, so my next idea was to bring the proper window to the front before triggering the click. It works but ends in a total mess... ;-)

+1  A: 

You can use probably use the Accessibility APIs.

It's a bit more complicated, but it should work.

NilObject
As far as i know, you can only send AXAktion items. No mouse clicks.
Why does it have to be a mouse click specifically? Sending AXPress to, say, a button should work just fine.
Peter Hosey
This does work, but note that you should be careful to test if the application window is hidden; it can break accessibility in my experience.
Nicholas Riley
The button is a graphical element and has no AXAktions.
A: 

Maybe you can use the Window Manager API: SetUserFocusWindow() and then create the mouse event.

I already did this via AXAction: "AXRise". But it ends in a flickering mess of windows.
+2  A: 

It is possible to send events to Cocoa applications via the undocumented

CGEventPostToPSN

Here is some code sample from Dave Keck. He posted a little application on the mailing list.

customEvent = [NSEvent mouseEventWithType: [event type]
                                 location: [event locationInWindow]
                            modifierFlags: [event modifierFlags] | NSCommandKeyMask
                                timestamp: [event timestamp]
                             windowNumber: WID
                                  context: nil
                              eventNumber: 0
                               clickCount: 1
                                 pressure: 0];

CGEvent = [customEvent CGEvent];
CGEventPostToPSN(&psn, CGEvent);

To archive it, i pasted more source code on pastie.org

For reference: The whole thread on cocao-dev mailing list