Re: Implementing behavior for "make" appleevent in Cocoa App
Re: Implementing behavior for "make" appleevent in Cocoa App
- Subject: Re: Implementing behavior for "make" appleevent in Cocoa App
- From: "Neal A. Crocker" <email@hidden>
- Date: Sun, 2 Sep 2001 21:27:59 -0700
Thanks for taking the time to reply, Brian.
Date: Sun, 2 Sep 2001 19:12:56 -0500
From: Brian Webster <email@hidden>
Subject: Re: Implementing behavior for "make" appleevent in Cocoa App
Cc: email@hidden, email@hidden,
"Neal A. Crocker" <email@hidden>
To: email@hidden
On Sunday, September 2, 2001, at 09:40 AM, cocoa-dev-
email@hidden wrote:
> As I understand it, if the decription of an object in an
> app.'s .scriptSuite file fails to specify a method to handle the
> create event and the object inherits from NSObject (as is the case
> for the the application/NSApplication object in Sketch), an
> NSCreateCommand object is created when a create event is sent to the
> application object.
The make command is a special case of sorts since it doesn't have a
direct object argument like most other commands. Commands with a
direct object can have the default behavior overridden by specifying
a key in the SupportedCommands key of the direct object's class.
For example, if you write an Applescript that says "close front
window", NSWindow handles the command by specifying the
handleCloseScriptCommand: selector in its entry scripting
dictionary and then implementing that method.
But, if there is no selector specified, then the
performDefaultImplementation: method in the corresponding command
class will be called instead. With NSCreateCommand, I think this
will always be the case, as it does not have a direct object and so
the default behavior can't be overridden by that mechanism.
I'm not absolutely posittive, but I believe create/make commands are
actually sent to the current Applescript "tell target" (assuming that
that object has a method to handle it), which must, of course be able
to contain the kind of object which is to be created. For instance,
suppose the .scriptSuite file of the application specifies
NSCoreSuite.Create as a supported commmand for the
application/NSApplication object, indicating that it is to be handled
by the "handleCreateCommand:" method. Suppose also that
NSApplictation is given a "handleCreateCommand:" method (by using the
categories mechanism, or subclassing). The following Applescript
would cause a create/make event to be sent to the
application/NSApplication object (thus invoking
"handleCreateCommand:" method of NSApplication object) asking for the
creation of a document/(NSDocument subclass) object:
tell application "Some Cocoa App.app"
make new document at before front document
end tell
This hypothesis can be easily tested, of course, and I mean to do it
eventually.
> If no other handling options exist, and
> NSCreateCommand implements performDefualtImplementation:, this method
> is invoked to handle the create event. So, does NSCreateCommand
> implement this method? If so, what does this method do? Also, where
> is it documented.
I'll answer the last question first: it's not documented anywhere. :(
As far as I can tell, NSCreateCommand does something along these lines:
1. Creates a new object of the given class. There's nothing
special to this; it just does [[theClass alloc] init].
2. Sets the properties of the object specified in the "with
properties {...}" clause of the AS command. It's basically
equivalent to a bunch of set data events being executed, except
they're all done in one fell swoop.
3. Inserts the newly created object in its container as
appropriate. For instance, when executing "make new document at
end of documents", it will insert the new document into its
container (the application object) using key-value coding. Look
in NSScriptKeyValueCoding.h for the "documentation" on the
method names that can be used to perform insertion and deletion
of objects.
Like I said, this isn't documented, but should be. I've just
pieced it together from some stack traces and extrapolation.
I played around with the Sketch example project and the debugger
after my original post on this thread what I saw is entirely
consistent with your conclusions about NSCreateCommand.
Specifically, I put a break in the init method for the Sketch
document class and used Applescript to send a "make new document"
event to Sketch. Near as I can tell, NSCreateCommand does have a
(totally undocumented) "performDefaultImplementation:" method which
eventually calls the "init:" method for an instance of the kind of
object to be created.
> Before the hypothetical
> performDefualtImplementation: method of NSCreateCommand is invoked,
> an attempt is made to locate a method to handle the NSCreatedCommand
> using something called key-value coding.
Key-value coding is not used to locate method names for handling
scripting commands. These names are obtained only from the
scripting dictionary for the app (and the dictionaries of the
frameworks it uses, of course).
Actually, there's a tantalizing in the developer documenation that
Key-value coding can be used to locate method names for handling
scripting commands at
file://localhost/Developer/Documentation/Cocoa/ProgrammingTopics/Scripting/ScriptableApps/ScriptableApps.d.html#pgfId=1002543
(Point your browser to the above link watch out for the line wrap in the URL.)
Of particular interest, this documentation page says:
"The description of a class (in a suite definition) includes a
section for commands supported by the class. In this section you can
either indicate that the default implementation (WHICH IS BASED ON
KEY-VALUE CODING) for a command is sufficient, or you can specify a
method that you want to handle the command when it is executed...."
(The emphasis supplied by using all caps is mine.)
--
Brian Webster
email@hidden
http://www.owlnet.rice.edu/~bwebster
Thanks,
Neal