如何着色嵌套复合路径Extendscript for Illustrator CS5?

时间:2014-11-05 16:54:54

标签: javascript adobe-illustrator extendscript

我试图在Illustrator中为pathItems内的compoundPathItem着色。我的对象看起来像圆圈内的圆圈。 compoundPathItems内没有笔画,四条路径,两条compoundPathItem

当我使用此行调试时:alert(doc.compoundPathItems [0])I get:-[CompoundPathItem] .... which makes sense because I've combined two compoundPathItems.

但是,我似乎无法访问pathItems以便为它们着色:

doc.compoundPathItems[0].compoundPathItems[0].pathItems ... errors out x_x ... 
alert(doc.compoundPathItems[0].pathItems.length) gives me 0 .... T-T

请帮助我了解这里发生的事情。

3 个答案:

答案 0 :(得分:2)

如果所有pageItems都是compoundItemsapp.activeDocument.pathItems将在文档中提供所有pathItems,则此脚本将有效。它还将嵌套在compoundPathItem中。所以试试它是否适合你

var currentDocument = app.activeDocument;
var pathItems = currentDocument.pathItems;
var redSwatch = currentDocument.swatches.getByName('CMYK Red');
var blueSwatch = currentDocument.swatches.getByName('CMYK Blue');
for (var i = 0; i < pathItems.length; i++) {
    pathItems[i].filled = true;
    pathItems[i].fillColor = redSwatch.color;
    pathItems[i].stroked = true;
    pathItems[i].strokeColor = blueSwatch.color;
}

答案 1 :(得分:0)

是对象的形状,如字母“O”?或“◎”&gt;复合路径项中的两个compoundPathItem

我认为最后为compound_path_item着色的所有path_item都填充了颜色父对象(= compound_path_item)。

如果字母“O”,请尝试以下代码

#target "Illustrator"
var doc = app.documents[0];
var co_pathitem = doc.compoundPathItems[0].pathItems;

// create colors
var color1 = new RGBColor();
color1.red = 255;

var color2 = new RGBColor();
color2.blue = 255;

// fillcolor red
co_pathitem[0].filled = true;
co_pathitem[0].fillColor = color1;
$.writeln(co_pathitem[0].fillColor.properties()); // => color1

// fillcolor blue
co_pathitem[1].filled = true;
co_pathitem[1].fillColor = color2;
$.writeln(co_pathitem[1].fillColor.properties()); // => color2

// => doc.compoundPathItems[0] will be colored with color2(colored at last)

// but pathItems[0] filled with color1 yet
$.writeln(co_pathitem[0].fillColor.properties()); // => color1

Object.prototype.properties = function (cr) {
  var self = this;
  var cr = cr || ", ";
  var props = [];
  for (var i in self) {
    try {
      props.push(i + ":" + self[i]);
    } catch (e) {
      // props.push(i + ":" + e);
    }
  };
  return props.join(cr);
}

答案 2 :(得分:0)

重点是:如果要解压缩目标compoundPathItem,则不仅会得到很多pathItem。您将在子组中获得groupItems,另一个compoundPathItems,pathItems等。整个结构与创建compoundPath之前的状态相同。获取所有pathItems的唯一方法是doc.pathItems。但是您可以做到这一点,并检查compoundPathItem.layer(它的父层)和每个pathItem的父层是否在同一层。所以,这对我来说很完美:

var whiteColor = new CMYKColor();
whiteColor.black = 0;

var doc = app.activeDocument;
//....defining targetLayer here
var whitePathItem = targetLayer.compoundPathItems[0]; //Target layer contains our compound Path
var parentLayer = whitePathItem.layer;

for(var i = 0; i < doc.pathItems.length; i++) {
    var pathItem = doc.pathItems[i];
    if(pathItem.layer == parentLayer) pathItem.fillColor = whiteColor;
}

只需检查父层,这就是窍门!