Как получить целое число PathItems?(Сценарий иллюстратора) - PullRequest
0 голосов
/ 23 января 2019

Я создал код для применения случайного цвета к контуру в Illustrator.Тем не менее, это относится только к первому пути слоя, игнорируя мой выбор.Я знаю, что это происходит потому, что pathItems [] равен 0. Если он равен 1, он перекрашивает второй путь и т. Д.

Как узнать целое число моего текущего выбора?Поэтому я могу сохранить ее как переменную AnyNumber и заменить docRef.pathItems [0] на docRef.pathItems [ANYNUMBER].

Ссылка на PathItems здесь .

Спасибо.

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 голосов
/ 24 января 2019

Я нашел решение, создав массив.

Вот весь сценарий, если кто-то в будущем захочет проверить код:

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

}}}
...