In Objective-C one can add a bunch of methods to a class by adding a category.
Is it possible to do the same where the new added methods reside in an AppleScript file?
I guess not, but it would have been a convenient way of breaking up a script file into smaller units.
An example: In a document-based project, menu items in the MainMenu.xib are connected in IB to methods listed in First Responder, which lists all available one-argument methods in all class files in the project. Normally, the methods connected to custom menu items all reside in a single file which is the File’s Owner in the MyDocument.xib.
I would like to connect a menu item to a method that is in another script file, but it is going to deal with the same window and document as the File’s Owner deals with. It is easy enough to connect the menu item to such a handler in an “extra” script file, but I can’t make it “active” (it remains gray). Something is missing (and I have a gnawing feeling that it’s something obvious.)
What is the elegant solution to this?
No, you can’t make AppleScript categories.
I wonder if your problem is with validateMenuItem_ – if you have that in your document class, it’s not going to know about your new class.
I also suspected the validateMenuItem_, but I tried to simply return true from that handler, but it did not solve the problem.
Is the new script file in the same xib as the document script?
Well, I have experimented with various ways. Generally, I don’t clearly see why it needs to belong to any particular xib. It is just a bunch of handlers that would like to get called by menus and then set some data. But I have had a blue cube in the xib set to the class of the new file. But I don’t see the point of that, since there is nothing I bind to things in that cube, except possibly an outlet to the window. But first I have to make at least that menu to react in some way.
I have also had the new file being a subclass of MyDocument. I have also had statement in MyDocument make an instance by aloc’s init, but I don’t fully understand what purpose that would serve. Somehow, I need to “connect” myDocument with the new file. I know it sounds like a sloppy way of expressing things, but I don’t see things clearly here. That’s why I had that mental picture of categories, since that would have been a model I can understand – categories just add. The new file should be a “trailer” to the “big” file:-) (or perhaps a dinghy:-)
Well, I have experimented with various ways. Generally, I don’t clearly see why it needs to belong to any particular xib. It is just a bunch of handlers that would like to get called by menus and then set some data. But I have had a blue cube in the xib set to the class of the new file. But I don’t see the point of that, since there is nothing I bind to things in that cube, except possibly an outlet to the window.
Action methods are instance methods, so you can only call them on objects, not classes. It sounds like you might be using nil-targeted actions, where no actual target is specified, and the menu items are not being enabled because no such method is found in the document responder chain.