Result type: how to return a list?


(Phil Stokes) #1

In my sdef I have some commands (subclasses of NSScriptCommand) from which I need to return lists and records.

To my surprise, the built-in support for AppleScript return types does not include lists. Records are also advised against, but I think I can handle building my own record-type elements (at least it seems straightforward enough).

However, most of the time, I just want to return a list (possibly of mixed types). I can’t find any examples of how this is done in the sdef’s on my own machine.

I note the docs mention that you can create your own value-types, but I’m not sure I fully understand how these work. Can I create my own return type for lists just by doing this (I’m assuming if it was that easy they’d be included to start with, but presumably I’m missing something – see Edit below)?

<value-type name="List" code="LisT">
    <cocoa class="NSArray"/>
</value-type>

And if not, is there some other way I might achieve this?

Edit:
Trying it gave me this error message:

Error while returning the result of a script command: the result object…
(
hello,
world
)
could not be converted to an Apple event descriptor of type ‘List’. This instance of the class ‘Swift._SwiftDeferredNSArray’ doesn’t respond to -scriptingListDescriptor messages.


(Shane Stanley) #2

There’s built-in support for lists. You use a type element with a list attribute, for example:

			<result description="The description.">
				<type type="text" list="yes"/>
			</result>

And in this case you return an array of strings.

For a mixed list, you use a type of any. In that case I think you have to provide an array of descriptors for the values.


(Phil Stokes) #3

Ah, right, thanks.

Reading up from where the docs mention results specifically and talk about parameters in general, I do now see an incidental mention of that. I expect I’d have continued to overlook it if you hadn’t pointed it out.

Edit:
Yay, bingo! Thx muchly (or is it ‘bigly’ these days? :rofl:)

24


(Jonas Whale) #4

Do you know Sdef Editor?
It has not been updated since 5 years but it’s still running on El Capitan without any issue.
:wink:


(Phil Stokes) #5

Yeah, thanks, I’d tried it at some point in the past.

I don’t really need an sdef editor, I just needed to know the syntax for the result parameter, which Shane has kindly provided.


(Jonas Whale) #6

I do not pretend to teach you anything about it, nor to compete with Shane.
I just wanted to share (with you and others) a tool that saves me a lot of time…
:wink:


(Phil Stokes) #7

Yes I understand that, but it wouldn’t have helped in this case. I did find that I had a copy sitting around on an archive and found that it wouldn’t have been able to provide the syntax for either lists or records in the results parameter.

But the suggestion was welcome, nonetheless!

(And the good news is I’ve got my records working now, too; and it looks like I should be able to use record-types to instantiate a mixed list as well, so all’s good!). :smiley: