1

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... ;-)

3 Answers3

4

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

1

You can use probably use the Accessibility APIs.

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

Ecton
  • 10,702
  • 2
  • 35
  • 44
  • As far as i know, you can only send AXAktion items. No mouse clicks. –  Apr 07 '09 at 18:28
  • Why does it have to be a mouse click specifically? Sending AXPress to, say, a button should work just fine. – Peter Hosey Apr 07 '09 at 18:56
  • 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 Apr 08 '09 at 13:03
  • The button is a graphical element and has no AXAktions. –  Apr 08 '09 at 20:12
1

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

user39113
  • 297
  • 2
  • 4
  • I already did this via AXAction: "AXRise". But it ends in a flickering mess of windows. –  Apr 07 '09 at 18:32