Defining and Running Simple AppleScript Commands in a Cocoa Application

I am trying to add some scripting functions to a Cocoa application that I wrote. I created a sdef (Scripting Definition File) file for my project. So far, I have managed to access object children (elements) using AppleScript, but I can’t understand for the rest of my life how to call methods (commands).

Here is my sdef file.

<suite name="mySuite" code="mSUI" description="My Application Suite"> <class name="application" code="capp" description="Top level scripting object."> <cocoa class="NSApplication"/> <!-- I can access these elements fine --> <element description="Test children." type="child" access="r"> <cocoa key="myChildren"/> </element> <!-- Cannot seem to call this method :( --> <responds-to command="testmethod"> <cocoa method="exposedMethod:"/> </responds-to> </class> <class name="child" code="cHIL" description="A Child." plural="children"> <cocoa class="Child"/> <property name="name" code="pnam" description="The child name." type="text" access="r"> <cocoa key="name"/> </property> </class> <command name="testmethod" code="tEST" description="Execute the test method" /> </suite> 

Here are my implementations of the controller class (this is the delegate of my application)

Mycontroller.h

 #import <Cocoa/Cocoa.h> @interface MyController : NSObject { NSMutableArray* myChildren; } // Some Methods @end 

MyController + Scripting.m

 #import "MyController+Scripting.h" @implementation MyController (Scripting) // This works when I'm accessing the myChildren - (BOOL)application:(NSApplication*)sender delegateHandlesKey:(NSString*)key { NSLog(@"Key = %@", key); return ([key isEqualToString:@"myChildren"]); } // This does NOT work... - (void)exposedMethod:(NSScriptCommand*)command { NSLog(@"Invoked Test Script Method %@", [command description]); } @end 

Lastly, the AppleScript I'm trying to do:

 tell application "MyApplication" testmethod end tell 

which answers "AppleScript Error - The variable testmethod is not defined."

Any ideas what I'm doing wrong here? I feel like I'm missing something simple, but my Googling doesn't seem to help anything.

+6
objective-c cocoa applescript
source share
3 answers

Basically everything looks right, but the code for <command/> should be two-component code (eight characters), not four.

+2
source share

I just posted a detailed solution on how to add a command with an argument here: fooobar.com/questions/167477 / ...

In short: I think you should subclass NSScriptCommand and override - (void) execute DefaultImplementation, and not expose the method in your controller. At least what I drove from docs :

"This command is a subclass of NSScriptCommand containing one method, executeDefaultImplementation. This method overrides the version in NSScriptCommand."

... and it works great for me, see my related answer for details.

+2
source share

(Actually, I never added scripting capabilities to the Cocoa app, so take my hit in the dark with salt.)

The first thing I guess is that exposedMethod: accepts a parameter, but I don't see where you can specify in your sdef or AppleScript. Actually, it seems AppleScript treats testmethod as a variable, not a command. (Maybe it should be something like " testmethod with ... " instead?) Perhaps you will need to define a <parameter> or <direct-parameter> element for the command in sdef.

Also, don't you need an object to call a method? Since your controller is the delegate of the application, I am not sure of its subtleties, but can AppleScript try to call testMethod: in NSApplication instead of your delegate?

I assume you looked at the Apple code sample and other resources, but if not, here are some links that might help:

Good luck

+1
source share

All Articles