可可脚本:返回从克隆对象"重复"命令 [英] Cocoa Scripting: Returning the cloned objects from a "duplicate" command

查看:300
本文介绍了可可脚本:返回从克隆对象"重复"命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该AppleScript的复制命令应该返回复制的对象。

The AppleScript duplicate command is supposed to return the copied objects.

和同时使用原来的基础AE-功能的应用程序似乎要做到这一点的基础上,可可脚本框架的应用程序似乎永远不会返回任何东西,但的缺失值

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.

看来的命令处理程序 NSCloneCommand 负责不返回符用于克隆的对象。

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.

它也可以,如果多个项目一起在使用参数与命令(如复制每个部件结束克隆):那我可以返回类型的说明 NSRangeSpecifier ,指定这些克隆项目的第一个和最后一个

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.

然而,如果使用在多个项目的复制命令的没有的的参数,然后该项目得到分类到阵列中的非连续的方式即可。举例来说,如果有最初2X元素,id为1和2,复制每x 之后原来将插入每个元素的副本,让我们马上让他们在这个顺序:1,3,2,4

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.

现在,如何总会有回报的这样的说明,即对项目3和4的说明符?

有在 NSScriptObjectSpecifier 的子类没有名单说明,我不能为每个单独的 NSScriptObjectSpecifier 或者,它似乎。虽然 NSAppleEventDescriptor 支持列表的创建,我想不通我怎么对象符转换成NSAppleEventDescriptors。

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.

我怎样才能解决这个以外的其他强制执行克隆对象的连续顺序(这需要我完全重新实现 NSCloneCommand 的操作,我害怕)。

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,马克Aldritt ,脚本调试程序的作者的确认这个问题复制(另:移动打开),因为他们应该不返回值。

BTW, Mark Aldritt, author of Script Debugger, confirms the issue that duplicate (also: move, open) do not return values as they're supposed to.

推荐答案

马克Aldritt 帮我远一点,告诉我一些私人API方法:

Mark Aldritt helped me a little further, telling me about some private API methods:

@interface NSScriptObjectSpecifier (NSPrivate)
+ (id) _scriptingSpecifierWithDescriptor:(NSAppleEventDescriptor*) descriptor;
+ (id) _objectSpecifierFromDescriptor:(NSAppleEventDescriptor*) descriptor inCommandConstructionContext:(id) context;
- (NSAppleEventDescriptor*) _asDescriptor;
@end

_asDescriptor 是我一直在寻找 - 一个办法把一个对象说明符成 NSAppleEventDescriptor 使我可以添加到列表对象。在code表示应该是这样的:

The _asDescriptor was what I was looking for - a way to turn an object specifier into a NSAppleEventDescriptor so that I can add that to a list object. The code for that would look like this:

- (NSAppleEventDescriptor*) objectSpecifiersAsList:(NSArray*) objectSpecifiers {
    NSAppleEventDescriptor* result = [NSAppleEventDescriptor listDescriptor];
    for (NSScriptObjectSpecifier* specifier in objectSpecifiersArray) {
        [result insertDescriptor:specifier._asDescriptor atIndex:0];
    }
    return  result;
}

当我尝试这样做,返回非连续的条目,我发现了,但是,这是行不通的。事实上,它已经为返回相同效果的的NSArray 相同 NSScriptObjectSpecifier 取值。这里有一个例子:

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 same NSScriptObjectSpecifiers. Here's an example:

set x to duplicate widgets 1 thru 2

通过自定义重复命令处理程序返回符为复制的项目3和4的名单,AppleScript的最终调用相同的命令处理程序第二次之后,它给出了错误-10006与消息:

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:

Can't set widgets 1 thru 2 to widgets 1 thru 2

你要知道 - 它并没有说部件3直通4或{3小部件,部件4}。不,它总是在报告的第一个参数复制命令给予的项目。

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.

当我改变我的code到返回单个符或一个范围说明,该命令再次正常行为。

As soon as I change my code to returning a single specifiers or a range specifier, the command behaves normally again.

所以看起来这是可可脚本一个隐藏的错误(或AppleScript的?),其中它不能处理在列表中返回的对象说明符。

So it seems like this is a hidden bug in Cocoa Scripting (or AppleScript?) wherein it cannot handle returned object specifiers in a list.

更新和放大器;解决方案

更多的尝试和错误之后,我想通了,工作的方式:

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:


  • 要使用code上面返回 listDescriptor ,结果类型需要任何,即:

  • To use the code above that returns a listDescriptor, the result type needs to be "any", i.e.:

<result>
    <type type="any"/>
</result>


  • 另外,如果结果类型更改为任何名单,那么就可以返回一个包含 NSAppleEventDescriptor 值一个NSArray:

  • Alternatively, if the result type is changed to "list of any", then one can return an NSArray containing the NSAppleEventDescriptor values:

    <result>
        <type type="any" list="yes"/>
    </result>
    


  • 这两种解决方案需要使用专用 _asDescriptor 的方法,但是,由于没有其他把一个脚本化的对象变成 NSAppleEventDescriptor已知的方法

    Both solutions require the use of the private _asDescriptor method, however, as there is no other known way to turn a scriptable object into a NSAppleEventDescriptor.

    (当然,如果你的应用程序支持复制命令只有一个类型的元素,则可以更改为yourtype的名单,只是一个返回类型你的对象的NSArray,而不需要私有方法 - 这是唯一需要的返回类型的结果任何

    (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 type any.)

    马克说这有关使用私有方法:

    Mark says this about using the private method:

    如果您担心Mac App Store的问题,这些私有方法被苹果给我,因​​为没有替代API。我是pretty确保您能获得许可使用它们。

    If you are concerned about Mac App store issues, these private methods were give to me by Apple as there is no alternative API. I’m pretty sure you can get permission to use them.

    我希望能够提交自己的应用程序很快实施这一解决方案的应用程序商店。然后,我将使用私有函数的结果更新这个答案。

    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.

    这篇关于可可脚本:返回从克隆对象&QUOT;重复&QUOT;命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆