title: Defining and executing simple AppleScript commands in a Cocoa app
body: <p>I'm trying to add some scripting functionality to a Cocoa app that I've written. I've created an <strong>sdef</strong> (Scripting Definition File) for my project. So far I have been successful in accessing object children (elements) with AppleScript but I cannot for the life of me figure out how to call methods (commands).</p>
  
  <p>Here is my sdef file.</p>
  
  <pre><code>&lt;suite name="mySuite" code="mSUI" description="My Application Suite"&gt;
      &lt;class name="application" code="capp" description="Top level scripting object."&gt;
          &lt;cocoa class="NSApplication"/&gt;
          &lt;!-- I can access these elements fine --&gt;
          &lt;element description="Test children." type="child" access="r"&gt;
              &lt;cocoa key="myChildren"/&gt;
          &lt;/element&gt;
          &lt;!-- Cannot seem to call this method :( --&gt;
          &lt;responds-to command="testmethod"&gt;
              &lt;cocoa method="exposedMethod:"/&gt;
          &lt;/responds-to&gt;
      &lt;/class&gt;
      &lt;class name="child" code="cHIL" description="A Child." plural="children"&gt;
          &lt;cocoa class="Child"/&gt;
          &lt;property name="name" code="pnam" description="The child name." type="text" access="r"&gt;
              &lt;cocoa key="name"/&gt;
          &lt;/property&gt;
      &lt;/class&gt;
      &lt;command name="testmethod" code="tEST" description="Execute the test method" /&gt;
  &lt;/suite&gt;
  </code></pre>
  
  <p>Here are my controller class implementations (this is the delegate of my application)</p>
  
  <p><strong>MyController.h</strong></p>
  
  <pre><code>#import &lt;Cocoa/Cocoa.h&gt;
  
  @interface MyController : NSObject {
      NSMutableArray* myChildren;
  }
  // Some Methods
  
  @end
  </code></pre>
  
  <p><strong>MyController+Scripting.m</strong></p>
  
  <pre><code>#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
  </code></pre>
  
  <p>Lastly, the AppleScript I am trying is:</p>
  
  <pre><code>tell application "MyApplication"
      testmethod
  end tell
  </code></pre>
  
  <p>which responds with <code>"AppleScript Error - The variable testmethod is not defined."</code></p>
  
  <p>Any ideas what I'm doing wrong here? I feel like I'm missing something simple but my Googling doesn't seem to be turning up anything helpful.</p>