[Qt-interest] [OS X] Make Qt app AppleScriptable?

Till Oliver Knoll till.oliver.knoll at gmail.com
Tue Aug 9 11:05:48 CEST 2011


Hello,

How does my AppleScript command "Open" return a "Document value"
(NSDocument) from my Qt application?


I am trying to teach my Qt Cococa application some AppleScript. I
successfully added a MyApp.sdef file which includes the "standard
Cocoa commands" (like Open, Print, Close, ...) by following the
example:

  http://developer.apple.com/library/mac/#samplecode/SimpleScriptingVerbs/Introduction/Intro.html

On my "Lion" installation the file
"/System/Library/ScriptingDefinitions/CocoaStandard.sdef" (the one
which is included my my *.sdef file) defines the "Open" command to
return a "Document" (which is implemented by the Cocoa class
"NSDocument").


Additionally I turned on AppleScript in the Info.plist file, where I
also specified the MyApp.sdef file.

My application also listens for "Open" events, such that when I
double-click on a registered document my application is started and
opens the document (by reacting to Apple Open events:
http://doc.qt.nokia.com/latest/qfileopenevent.html).


So what is working is that

a) Apple Script Editor recognises the available AppleScript commands
when inspecting my application

b) The following script almost works:

  tell "MyApp"
    open "/some/document.txt"
  end

My application indeed opens the document, but the Script returns with
the error "missing value" - the expected "Document" value, I guess.

I noticed that there is no NSOpenCommand which I could "override" (in
analogy to e.g. NSMoveCommand). To my understanding the AppleScript
"Open" command is translated directly to some "Open" Apple Event
(which is indeed received by my application).

So my guess is that a "native" Cocoa application (which is based on a
NSDocument) automagically handles that "Open" command and returns an
"object description" of the just created NSDocument.


Could I simply add some "dummy" NSDocument based class each time I
create my actual (Qt based) document model? But how would Cocoa figure
out which instance of NSDocument to return? Do I implement some
special Cocoa delegate class for NSApplication, or add methods (with
Objective-C "Categories") to the NSApplication class itself where I
would return the "dummy" NSDocument instance (which would have a
reference to the "real" Qt based document)?

Somehow I believe the answer lies somewhere here in "Use the Document
Architecture":

  http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_implement/SAppsImplement.html#//apple_ref/doc/uid/20000037-CJBEBEEB

"The NSApplication, NSDocument, NSDocumentController, NSWindow, and
NSWindowController classes form the basic structure for the Cocoa
document architecture. Together with the terminology defined in the
Standard suite, these classes provide direct support for the standard
AppleScript document scripting model, including classes such as
application, document, and window. [...] NSApplication provides
methods for accessing the application's documents as an ordered list."


So sounds like that when I open a document I need to create a "wrapper
NSDocument" and somehow add this to the ordered list of the
NSApplication, and then when I have handled the "Open" event Cocoa
would somehow figure out itself which instance of NSDocument (the
"object description" thereof actually) to return? Sounds a bit
sketchy...



Googling for "Qt AppleScript" is kind of hard (guess what you'll find
mostly ;) - So does anyone have some experience with Qt (as in "the
C++ framework") and AppleScript and the standard Cocoa commands,
specifically "Open"?


Thanks, Oliver



More information about the Qt-interest-old mailing list