Here's what I have:
1) Include the proper "permissions" entries in the 'Info.plist
- 'Scriptable': YES
- 'Scripting definition file name': myApp.sdef
2) Include the element "element" tag within a class extension "element" tag:
`<class-extension extends="application" description="The application and top-level scripting object.">
<!-- various property tags go here -->
<element type="object item" access="r">
<cocoa key="theseObjects"/>
</element>
</class-extension>`
3) Include the element class tag:
<class name="object item" code="Objs" description="Application 'too many' object collection" plural="object items" inherits="item"> // I don't believe 'inherits' name is critical for AS to work
<cocoa class="ObjectItem"/>
</class>
4) Include the delegate method that forwards 'NSApplication' scriptability support to its delegate:
- (BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString *)key {
if ([key isEqualToString:@"theseObjects"]) {
return YES;
}
return NO;
}
5) Create a 'ObjectItem' class and put the object specifier there:
- (NSScriptObjectSpecifier *)objectSpecifier {
NSScriptObjectSpecifier *containerRef = nil;
NSScriptObjectSpecifier *specifier = [[NSNameSpecifier alloc] initWithContainerClassDescription:[NSScriptClassDescription classDescriptionForClass:[NSApp class]] containerSpecifier:containerRef key:@"theseObjects" name:@"objectName"];
return [specifier autorelease];
6) Post the KVO accessor method within the Application's delegate:
- (NSArray *)theseObjects;
{
ObjectItem *thisObject = [[ObjectItem new] autorelease];
NSArray *thisArray = [NSArray arrayWithObject:thisObject];
return thisArray;
}
}
7) Create an AppleScript that returns objects from my element getter method:
tell application "SpellAnalysis"
get theseObjects
end tell
8) The result: error "The variable objects is not defined." number -2753 from "objects"
9) Pull my hair out
Regarding the original One-to-many post, the author did a good job outlining the steps to get to the point where scripting almost works. Fortunately, only a few small changes are needed to fix the problem:
1) The sample AppleScript
can't possibly work. An AS can't refer to Objective-C variable names (theseObjects). Since the sdef defines a class and element type of "object item", that is what has to be used in the script:
2) The object specifier in the ObjectItem class should return the actual name property value of the current object, not the string @"objectName". So it should be:
3) The complete and correct sdef to support the above is:
4) The ObjectItem.h interface to support the above:
5) The ObjectItem.m file to support the above:
6) Finally, the code in your App delegate class to support the above:
That's it, just a couple of simple errors to fix (one in the script, one in the object specifier code), and the following script returns expected results:
Other scripts also work: