views:

195

answers:

3

I'm developing a Cocoa/Objective C application that reads the active document from any application using 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. 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.

I've tried looking at the other properties and attributes exposed through AppleScript of both an application process and a single window. 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. Am I just not looking hard enough? Or is there a good way of doing this in Objective C or AppleScript?

+1  A: 

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. Another idea would be to just look at the menu entries... are there File -> New, File -> Open menu entries?

Daniel Furrer
Thanks for the response. Sorry I was so late in rating your answer - I thought I'd be notified by email when answers were posted but this didn't happen. I think checking the menu entries are the best idea so far. Thanks for your answer.
John Gallagher
+1  A: 

You might want to look for the the CFBundleDocumentTypes key in the Info.plist of an application bundle. 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.

Adam Wright
Unfortunately, some of the apps I class as non-document are document apps in Apples definition. For example, I class iCal as being non-document - you can't have iCal documents. But Apple sees it as being able to open .ics files. So I can't use this technique, but thanks for the suggestion.
John Gallagher
In this case, the NSDocumentClass entry for the extension is probably empty - it is in iCals case.
Adam Wright
+1  A: 

Another idea would be to look for an AXImage representing the proxy icon in the title bar; untitled documents could simply be matched by checking the window title for "untitled" or its localized equivalent.

(Some apps do include a proxy icon even for untitled documents, but many don't.)

Nicholas Riley
Ah. Very interesting idea. Thanks.
John Gallagher