How would I programmatically activate i.e move-to-front-and-focus a window on macOS (not belonging to my app) given its Window ID. My app would run with user granted Accessibility permissions etc.
Surprisingly, none of the functions described on the Quartz Window Services page seem to do that.
Am using Swift currently, but am open to using Objective-C, AppleScript or whatever.
EDIT:
I don't want to bring to front all windows of the parent app - only the specific that matches the window ID.
Edit:
I know that the NSWindow type is only meant to refer to windows of the current process, but is there no class that represents windows owned by external apps? Like we have NSRunningApplication to refer to any running app including external ones, I was expecting an API to deal all open windows (assuming the right permissions). Is there some class like NSOpenWindow or CGWindow buried somewhere?
I didn't find a way to switch to a specific window yet, but you can switch to the app that contains a specific window using this function:
It is probably usefull to switch by name as well:
Example:
switchToApp(named: "OpenOffice")On my mac OpenOffice was started with a window with
kCGWindowNumber = 599, so this has the same effect:switchToApp(withWindow: 599)As far as I found out so far, your options seem to be to show the currently active window of the app, or to show all windows (using
.activateAllWindowsas activation option)