如何获取 PathItems 的整数个数? (插画脚本)

How to get the integer number of PathItems? (Illustrator Script)

我创建了一个代码来将随机颜色应用到 Illustrator 上的路径。但是,它只适用于图层的第一条路径,忽略了我的选择。我知道它会发生,因为 pathItems[] 是 0。如果它是 1,它会重新着色第二条路径,依此类推。

如何找出我当前选择的整数?所以我可以将它存储为 "AnyNumber" 变量并将 docRef.pathItems[0] 替换为 docRef.pathItems[ANYNUMBER].

路径项的引用here

谢谢。

var docRef = app.activeDocument;


// Create color

var rgb; 
var rgb = new RGBColor();

var random1 = Math.floor((Math.random() * 255) + 1);
var random2 = Math.floor((Math.random() * 255) + 1);
var random3 = Math.floor((Math.random() * 255) + 1);

rgb.red = random1;
rgb.green = random2;
rgb.blue = random3;


// Create swatch

var swatch = docRef.swatches.add();

swatch.color = rgb;

swatch.name = "Random Color"; 


// Apply swatch

var pathRef = docRef.pathItems[0];

pathRef.filled = true;

pathRef.fillColor = swatch.color;

pathRef.stroked = false;


// Delete swatch

swatchToDelete = app.activeDocument.swatches[swatch.name];

swatchToDelete.remove();

我通过创建数组找到了解决方案。

这是整个脚本,如果以后有人想查看代码:

doc = app.activeDocument;
sel = app.activeDocument.selection;
selSwatch = doc.swatches.getSelected();

if (sel instanceof Array) {

    if(selSwatch.length != 0)

        for (i=0; i<sel.length; i++) {

            if(sel[i].typename == "PathItem" || sel[i].typename == "CompoundPathItem") {

                selobj = sel[i];
                selobj.filled = true;

                // Create color

                var rgb;
                var rgb = new RGBColor();

                var random1 = Math.floor((Math.random() * 255) + 1);
                var random2 = Math.floor((Math.random() * 255) + 1);
                var random3 = Math.floor((Math.random() * 255) + 1);

                rgb.red = random1;
                rgb.green = random2;
                rgb.blue = random3;

                // Create swatch

                var swatch = doc.swatches.add();

                swatch.color = rgb;

                swatch.name = "Random Color"; 

                // Apply swatch

                selobj.filled = true;

                selobj.stroked = false;

                selobj.fillColor = swatch.color;


                // Delete swatch

                swatchToDelete = doc.swatches[swatch.name];

                swatchToDelete.remove();

}}}