通过 NSRunningApplication 实例获取应用程序的主窗口
我正在观察 NSWorkspaceDidDeactivateApplicationNotification 通知以获取刚刚失去焦点的应用程序。我最终得到了一个 NSRunningApplication 实例,您可以从通知对象的 userInfo 字典键 - NSWorkspaceApplicationKey 中获取该实例。
我想我可以从通知中获取应用程序的主窗口,但我不确定从这里去哪里,因为 NSRunningApplication 似乎非常有限。任何帮助将不胜感激。
顺便说一句 - 我正在使用 MacRuby,但答案不需要在 MacRuby 中。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
传统上,苹果公司对这类事情一直相当保守。 NSRunningApplication 本身刚刚在 10.6 中引入,正如您所说,它有一些限制。根据您想要执行的操作,答案可能位于 辅助功能框架 或者它可能是 CGWindow API。您可以使用 NSRunningApplication 中的 processIdentifier 将其与这些 API 进行匹配。
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.
获取其他应用程序的主窗口非常困难;他们甚至不能保证是可可!它们可以是 Carbon 或 Java,或 Qt,或 Mono...所以你无法获得另一个应用程序的 NSWindow,除非你进行 hack。您可以尝试 辅助功能 API独立于所使用的框架获取其他应用程序的窗口信息等,但它不太容易使用。
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.
除非应用程序选择通过 AppleScript 支持或其他方式参与 IAC,否则您只需不要触摸它的窗口或您自己的堆空间之外的任何其他内容。
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.