Jim Matthews
Jim Matthews

Reputation: 1191

How to return a list of objects from an NSScriptCommand subclass's performDefaultImplementation?

I am adding a verb-first AppleScript command in a Cocoa application. The sdef command definition indicates that the command returns a list of text strings:

<command name="list names" code="ABCDLstN" description="return a list of names">
    <cocoa class="ListNamesCommand"/>
    <result type="text" list="yes" description="some names"/>
</command>

The ListNamesCommand class's performDefaultImplementation method returns an NSArray of NSString:

- (id)performDefaultImplementation {
    return @[@"name 1", @"name 2"];
}

The result is an exception:

2013-12-17 17:22:37.474 ListNames[31907:303] Error while returning the result of a script command: the result object...
(
    name1,
    name2
)
...could not be converted to an Apple event descriptor of type 'text'. This instance of the class '__NSArrayI' doesn't respond to -scriptingTextDescriptor messages.

A workaround is to change the sdef to specify the result type as any rather than text, and return an NSAppleEventDescriptor instead of an NSArray:

- (id)performDefaultImplementation {
    NSAppleEventDescriptor *list = [NSAppleEventDescriptor listDescriptor];
    [list insertDescriptor:[NSAppleEventDescriptor descriptorWithString:@"name 1"] atIndex:1];
    [list insertDescriptor:[NSAppleEventDescriptor descriptorWithString:@"name 2"] atIndex:2];

    return list;
}

However, this has the unfortunate side-effect of documenting the event (in the AppleScript Editor dictionary viewer) as returning any type.

Is there a solution that does not have that drawback?

Upvotes: 3

Views: 487

Answers (1)

regulus6633
regulus6633

Reputation: 19030

I think when you define the command the type should be "list of text". I've never seen the part about "list=yes". I'm no expert but I would remove that. Good luck.

Upvotes: 2

Related Questions