简体   繁体   English

indesign中的isPrototypeOf

[英]isPrototypeOf in indesign

Hi am relativly new to indesign scripting and would like to figure out if an object is a subtype of a class. 嗨,您是Indesign脚本设计的新手,想弄清楚对象是否是类的子类型。 Example: I want to iterate over all page items and take everything that is not a graphic: 示例:我想遍历所有页面项并接受所有非图形的内容:

layer = app.activeDocument.layers[layerIndex];

for (i = 0; i < layer.allPageItems.length; i++) {
  alert(layer.allPageItems[i].reflect.name)
  if(layer.allPageItems[i].isPrototypeOf (Graphic) ) {
    alert("Graphic");
  } else {
    ....
  }
}

howver the if nver matches. 如果nver匹配。 Are there any examples of how to use isPrototypeOf ? 是否有使用isPrototypeOf What do I have to do to test if an object is of a certain type or a subclass of it? 我该怎么做才能测试对象是某种类型还是其子类?

edit : To clarify, I am trying to test if I have an Instance of anything that inherited from Graphic. 编辑 :澄清一下,我正在尝试测试是否有从Graphic 继承的任何实例。

But as far as I can see now that seems to be impossible. 但据我现在所知,这似乎是不可能的。

You probably want the instanceof operator. 您可能需要instanceof运算符。

if (layer.allPageItems[i] instanceof Graphic) {
    alert("Graphic");
} else {
    ....
}

You could also use isPrototypeOf but you have to reverse the order and get the prototype itself, not the constructor. 您也可以使用isPrototypeOf但必须颠倒顺序并获取原型本身,而不是构造函数。 So it would look like this: 所以它看起来像这样:

if (Graphic.prototype.isPrototypeOf(layer.allPageItems[i])) {
    alert("Graphic");
} else {
    ....
}

You can get access to the essence of the pageItem by calling the getElements() method. 您可以通过调用getElements()方法来访问pageItem的本质。 It returns an array of the original material. 它返回原始材料的数组。 Given a rectangle on a page (nothing else) : 给定页面上的矩形(没有其他内容):

app.activeDocument.allPageItems[0].getElements()[0] instanceof Rectangle //true;

Are you sure its not supposed to be 你确定它不应该是

Graphic.isPrototypeOf(layer.allPageItems[i])

or something like 或类似的东西

Graphic.prototype.isPrototypeOf(layer.allPageItems[i])

?

Your current version sounds like its backwards. 您当前的版本听起来像是倒退了。

Apparently this is not possible, I also asked on the adobe Forums with this result: http://forums.adobe.com/message/4461211#4461211 显然这是不可能的,我也在adobe论坛上询问了以下结果: http : //forums.adobe.com/message/4461211#4461211

So the short answer is, I have no way to check if I hold an object wich is an instace of someClass or a child thereof. 因此,简短的答案是,我无法检查我持有的对象是否为someClass的someClass或其子级。 Neither reflection nor isPrototypeOf help. 反射和isPrototypeOf都不isPrototypeOf帮助。

I might try casting in a try catch block but consider this as ugly. 我可能会尝试在try catch块中进行投射,但认为这很丑陋。 Thus I will go with the solution suggested on the adobe Forums, test for all possible heirs (children/classes inherting from base) and the base class. 因此,我将使用adobe论坛上建议的解决方案,测试所有可能的继承人(从基层继承的子代/类)和基类。 This is ugly and lengthy but I have not found a better solution. 这是丑陋且冗长的,但我还没有找到更好的解决方案。

edit : here is an exceprt from one of adobes examples, it allows for the switch syntax avoidng an endless if construct: 编辑 :这是一个adobes示例的摘录,它允许使用switch语法避免无穷的if构造:

switch (app.selection[myCounter].constructor.name){
    case "Rectangle":
    case "Oval":
    case "Polygon":
    case "GraphicLine":
    case "TextFrame":
        myObjectList.push(app.selection[myCounter]);
        break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM