简体   繁体   English

如何检测应用程序是否基于文档?

[英]How do I detect if an application is document-based?

I'm developing a Cocoa/Objective C application that reads the active document from any application using AppScript.我正在开发一个 Cocoa/Objective C 应用程序,它使用 AppScript 从任何应用程序读取活动文档。 I've done this part successfully, but it would be really good to be able to tell for sure whether the application is document-based or not.我已经成功地完成了这部分,但是能够确定应用程序是否基于文档会非常好。

The obvious approach is to look at the AXDocument attribute of the active window within the application and if it contains a path, we know the app is document-based.显而易见的方法是查看应用程序中活动 window 的 AXDocument 属性,如果它包含路径,我们就知道该应用程序是基于文档的。 However, this doesn't work if the document is unsaved.但是,如果文档未保存,这将不起作用。 It's also pretty clumsy.它也很笨拙。 Rather annoyingly, the AXDocument attribute may appear in a non-document application, so it's presence or absence cannot be used to provide the information I seek.相当烦人的是,AXDocument 属性可能出现在非文档应用程序中,因此它的存在或不存在不能用于提供我寻求的信息。

I've tried looking at the other properties and attributes exposed through AppleScript of both an application process and a single window.我尝试查看通过应用程序进程和单个 window 的 AppleScript 公开的其他属性和属性。 None of these items seem to tell me for sure whether the app is document-based or not.这些项目似乎都不能确定该应用程序是否基于文档。

I've also tried looking through a few Info.plist files, but can't see anything that reliably indicates this.我也尝试过查看一些 Info.plist 文件,但看不到任何可靠地表明这一点的东西。 Am I just not looking hard enough?我只是不够努力吗? Or is there a good way of doing this in Objective C or AppleScript?或者在目标 C 或 AppleScript 中是否有这样做的好方法?

A document based application that is built according to the Document-Based Application Architecture guide should fulfill several conditions.根据基于文档的应用程序架构指南构建的基于文档的应用程序应该满足几个条件。 I'm not sure if/how it is possible to access different applications on Mac OS, but with reflection (in Objective-C) you could test for the presence of a NSDocument subclass.我不确定是否/如何可以访问 Mac OS 上的不同应用程序,但是通过反射(在 Objective-C 中),您可以测试是否存在 NSDocument 子类。 Another idea would be to just look at the menu entries... are there File -> New, File -> Open menu entries?另一个想法是只看菜单条目......是否有文件 - >新建,文件 - >打开菜单条目?

You might want to look for the the CFBundleDocumentTypes key in the Info.plist of an application bundle.您可能希望在应用程序包的 Info.plist 中查找 CFBundleDocumentTypes 键。 This is typically present for document based applications, so that the document type can be associated with both the extension, and the class that loads it.这通常存在于基于文档的应用程序中,因此文档类型可以与扩展名和加载它的 class 相关联。

Another idea would be to look for an AXImage representing the proxy icon in the title bar;另一个想法是在标题栏中寻找代表代理图标的 AXImage; untitled documents could simply be matched by checking the window title for "untitled" or its localized equivalent.可以通过检查 window 标题中的“无标题”或其本地化等效项来简单地匹配无标题文档。

(Some apps do include a proxy icon even for untitled documents, but many don't.) (有些应用程序确实包含一个代理图标,即使是无标题文档,但很多都没有。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM