Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the main window of an app via an NSRunningApplication instance

I'm observing NSWorkspaceDidDeactivateApplicationNotification notification to get the application that has just lost focus. I'm ending up with an instance of NSRunningApplication which you get from the userInfo dictionary key - NSWorkspaceApplicationKey - of the notification object.

I was thinking that I'd be able to get the main window from the app from the notification but I'm not sure where to go from here as NSRunningApplication seems to pretty limited. Any help would be appreciated.

BTW - I'm using MacRuby but the answer doesn't need to be in MacRuby.

Thanks

like image 762
Alistair Holt Avatar asked Dec 30 '09 17:12

Alistair Holt


2 Answers

Apple has traditionally been pretty locked-down about this sort of thing. NSRunningApplication itself was just introduced in 10.6, and as you said, it's a bit limited. Depending on what you want to do, the answer might be in the Accessibility framework or it might be the CGWindow API. You can use the processIdentifier from the NSRunningApplication to match it up with those APIs.

like image 181
Chuck Avatar answered Sep 22 '22 11:09

Chuck


It's very difficult to get the main window of other apps; they're not even guaranteed to be Cocoa! They can be either Carbon or Java, or Qt, or Mono... So there's no way you can get NSWindow of another app, unless you do a hack. You can try Accessibility API to get the window info etc. of other apps independently of the framework used, but it's not so easy to use.

like image 36
Yuji Avatar answered Sep 19 '22 11:09

Yuji