views:

233

answers:

3

Hello all,

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

+2  A: 

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.

Yuji
Thanks, I'll look into the accessibility API.
+1  A: 

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.

Chuck
Thanks, I'll have a look into both APIs.
A: 

Unless the application opts to participate in IAC via AppleScript support or some other means, you simply don't touch its windows or anything else outside of your own heap space.

Azeem.Butt