The AppleScript duplicate
command is supposed to return the copied objects.
And while apps using the original AE-based functions seem to do that, apps based on the Cocoa Scripting framework seem to never return anything but missing value.
It appears that the command handler of NSCloneCommand
is responsible for not returning the specifiers for the cloned objects.
I was attempting to fix this in my scriptable app by subclassing the command, collecting the cloned object specifiers and then returning them.
This works well if only one item is duplicated.
It also works if multiple items are cloned along with using the to
parameter with the command (as in duplicate every widget to end
): Then I can return a specifier of type NSRangeSpecifier
that designates the first and last of those cloned items.
However, if one uses the duplicate command on multiple items without the to
parameter, then the items get sorted into the array in a non-consecutive manner. For instance, if there are initially 2 "x" elements, with id 1 and 2, duplicate every x
will insert a copy of each element right after its original, so that we'll have them in this order: 1, 3, 2, 4.
Now, how would one return a specifier for this, i.e. a specifier for items 3 and 4?
There is no "list" specifier in the sub classes of NSScriptObjectSpecifier
, and I cannot return an NSArray for each individual NSScriptObjectSpecifier
either, it seems. And while NSAppleEventDescriptor
supports creation of lists, I cannot figure out how I'd convert the object specifiers into NSAppleEventDescriptors.
How can I solve this other than enforcing a consecutive order of the cloned objects (which would require me to re-implement the NSCloneCommand
's operation entirely, I'm afraid).
BTW, Mark Aldritt, author of Script Debugger, confirms the issue that duplicate
(also: move
, open
) do not return values as they're supposed to.
Mark Aldritt helped me a little further, telling me about some private API methods:
The
_asDescriptor
was what I was looking for - a way to turn an object specifier into aNSAppleEventDescriptor
so that I can add that to a list object. The code for that would look like this:When I tried this to return the non-consecutive items, I found, however, that this doesn't work. In fact, it has the same effect as returning an
NSArray
of the sameNSScriptObjectSpecifier
s. Here's an example:With the custom duplicate command handler returning a list of specifiers for the copied items 3 and 4, AppleScript ends up calling the same command handler a second time and after that it gives the error -10006 with the message:
Mind you - it does not say "widgets 3 thru 4" or "{widget 3, widget 4}". No, it always reports the items that were given at the first parameter to the duplicate command.
As soon as I change my code to returning a single specifiers or a range specifier, the command behaves normally again.
So it seems like this is a hidden bug in Cocoa Scripting (or AppleScript?) wherein it cannot handle returned object specifiers in a list.
Update & Solution
After more trial-and-error I figured out a way that works:
The type for the result has to be changed from "descriptor", and there are two possibilities:
To use the code above that returns a
listDescriptor
, the result type needs to be "any", i.e.:Alternatively, if the result type is changed to "list of any", then one can return an NSArray containing the
NSAppleEventDescriptor
values:Both solutions require the use of the private
_asDescriptor
method, however, as there is no other known way to turn a scriptable object into aNSAppleEventDescriptor
.(Of course, if your app supports the
duplicate
command for only one type of element, then you can change the type to "list of yourtype" and return simply an NSArray of your objects, without the need for the private method - that's only needed for returning results of typeany
.)Mark says this about using the private method:
I hope to submit my own app implementing this solution to the App Store soon. I shall then update this answer with the outcome of using the private function.