2

I've come across doing something like:

Code:

tell application "Preview"
    set p to {}
    set p to documents
end tell

Output:

{document "1st.pdf" of application "Preview", document "2nd.pdf" of application "Preview"}

But this is highly app-specific—doing this on MS Word, for instance, yielded pathnames and some apps like CodeRunner don't understand the documents keyword at all.

Given these open documents:

Current open documents from all open visible apps
+– Microsoft Word.app
|  +– 1.docx
|  +– 2.docx
+– CodeRunner.app
|  +– 1.sh
|  +– 2.py
|  +– 3.pl
+– Preview.app
   +– 1.pdf
   +– 2.png
.
.
.

I want an output of something like this:

{"~/Documents/1.docx", "~/Downloads/2.docx", "~/Dropbox/folder/1.sh", "~/Dropbox/others/2.py", "~/Downloads/folder/folder/3.pl", "~/Documents/special/1.pdf", "~/Dropbox/special/2.png", ...}

How is it possible to list all open documents across all open apps?

2

I’ve been trying to do this myself. I have concluded that there is no standard, guaranteed way to get the open documents of a running application.

What you tried is the best method. For apps that use the Cocoa document architecture (the majority), there is automatic AppleScript support for getting all the open NSDocument objects and querying certain properties of those objects. The mapping between the AppleScript properties and the Objective-C properties is shown below.

AppleScript property => Objective-C getter:

  • path => fileName
  • modified => isDocumentEdited
  • name => lastComponentOfFileName

What you want is the path property. Like tell application "Preview" to set documentsList to path of documents.

Unfortunately, this automatic AppleScript support is only available for apps that use the Cocoa document architecture and have AppleScript enabled (via the NSAppleScriptEnabled Info.plist key). Therefore, this method will not work on some apps. For example, CodeRunner does not have AppleScript enabled. And TextMate does not use the Cocoa document architecture.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .