title: Providing my own objects for "make new" in my scriptable application
body: <p>This is a question about implementing a scriptable application using Cocoa Scripting.</p>
  
  <p>My app's scriptable <code>application</code> object contains elements of a custom class, let call it <code>flower</code>.</p>
  
  <p>In the .sdef file, the Cocoa class for <code>flower</code> is specified as <code>ScriptableFlower</code>.</p>
  
  <p>In Applescript, one can now write:</p>
  
  <pre><code>tell app "myapp"
    get flowers
  end tell
  </code></pre>
  
  <p>My code provides an accessor function for retrieving flowers: <code>-(NSArray*)flowers</code>.</p>
  
  <p>Now, I like to implement a way to add new flowers, so that one can write:</p>
  
  <pre><code>tell app "myapp"
    make new flower
  end tell
  </code></pre>
  
  <p>The default behavior for this, with the default Core suite handler for "make" using <code>NSCreateCommand</code>, is as follows:</p>
  
  <p>The scripting engine will fetch the current array of flowers by calling my <code>flowers</code> function, then instantiate a new Cocoa object of class <code>ScriptableFlower</code>, and then call <code>setFlowers:(NSArray*)</code> with an array that contains my original objects plus the newly created one.</p>
  
  <p>However, this is not good for my application: I cannot allow the scripting engine to create objects of my scriptable classes at will.</p>
  
  <p>Instead, I need to be the one instantiating them.</p>
  
  <p>A half-way solution would be to implement the default <code>-(id)init</code> method and then detect if it's called by me - if not, I can take the extra steps. But that's not clean. I rather do not let the scripting engine create new objects at all but rather provide them myself as I may have the object "somewhere" already prepared.</p>
  
  <p>Is there some provision in Cocoa Scripting that leads to it calling me whenever it wants me to create a new scriptable object?</p>
  
  <p><strong>Update</strong></p>
  
  <p>To clarify: The <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ScriptableCocoaApplications/SApps_get_set/SAppsGetSet.html#//apple_ref/doc/uid/TP40002164-CH18-SW1" rel="nofollow">Cocoa Scripting docs</a> explains that one can implement special insertion handlers (<code>insertObject:in&lt;Key&gt;AtIndex:</code>)so that one doesn't have to take the entire NSArray, but that still leads to the scripting engine to create the object. I need to be asked to create the object instead, though.</p>