1

I'm working on a macOS app that needs to manipulate the windows of other applications, namely bringing another app's window to the front. Note that it's not enough to simply bring an application to the front (which is easily accomplished with NSRunningApplication) -- it needs to be a specific window.

Normally I would use the Accessibility API to do this, but unfortunately it's not reliable. Some applications do not respond to API requests in a timely manner or will return bogus or broken information, which can break my app. Consequently, I'm looking for alternative methods of doing this.

I'm open to using unofficial macOS APIs, which, while I know they are not recommended, I've found can often be a lot faster and more reliable. For example, I needed a method to figure out which window occupied a certain point on screen. While it is possible and supported to do this with the Accessibility API, it would often be very slow, or just break completely with apps that don't support Accessibility features correctly. But using the undocumented function CGSFindWindowAndOwner is much faster and more reliable, as it just queries the window server directly. I'm hoping I can find a similar method for raising a window too.

That said, if there is another supported method I'll certainly go for that, but I'm not aware of any other way of doing this without resorting to unofficial / undocumented APIs. (Maybe Apple Events can do it?)

Do I have any options other than trying to work around the bugs and failings of the Accessibility API?

Bri Bri
  • 2,169
  • 3
  • 19
  • 44
  • Have a look at this question: https://stackoverflow.com/questions/13760760/how-to-get-windows-of-nsrunningapplication. It should be possible to get all windows of a running application, but I'm not sure whether there is API to move a specific window into the foreground. Probably you have to examine the CoreGraphics APIs for methods that are not exposed on NSApplication etc. – mschmidt Jun 09 '23 at 20:24
  • I haven't found anything yet. There is `CGSOrderWindow` and `CGSOrderFrontConditionally` but if these functions worked at some point in earlier macOS releases, I sadly cannot figure out how to get them to work now. – Bri Bri Jun 09 '23 at 21:38
  • Did you try a mouse-down event? – Willeke Jun 10 '23 at 09:22
  • A mouse down event might work but has two serious downsides: it has side effects, even if you have it click something like the window's titlebar, and it only works if the window is not obscured. – Bri Bri Jun 12 '23 at 02:06

0 Answers0