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

时间:2019-01-23 18:11:44

标签: javascript adobe-illustrator

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

如何找出当前选择的整数?因此,我可以将其存储为“ AnyNumber”变量,并用docRef.pathItems [ANYNUMBER]替换docRef.pathItems [0]。

PathItems 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();

1 个答案:

答案 0 :(得分:0)

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

这是整个脚本,如果将来有人要检查代码:

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();

}}}