Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looping through array of pathItems only affects last item?

New Here ,
Jul 04, 2019 Jul 04, 2019

I'm writing a script to copy selected items onto another layer, then change the fill and stroke of the resulting items, so that they all have no fill, and the same stroke colour. It's mostly working – the duplication part works fine, but looping through an array of the resulting items in order to alter the properties of each item only seems to affect the last item in the array.

main();

function main() {

    //Check if there is a document

    if (app.documents.length == 0) {

        return;

    } else {

        //Check Cutter layer exists, get reference to it if it does

        var doc = app.activeDocument;

        var currentLayer = doc.activeLayer;

        if (existCheck(doc.layers, "Cutter")) {

            var cutterLayer = doc.layers.getByName("Cutter");

            //Get reference to selection (as Array)

            var mySelection = app.selection;

            //Iterate through array of selected objects, duplicating each one onto Cutter layer

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

                mySelection.duplicate(cutterLayer, ElementPlacement.PLACEATEND);

            }

            currentLayer.hasSelectedArtwork = false;

            var myPathItems = cutterLayer.pathItems;

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

                myPathItems.filled = false;

                myPathItems.stroked = true;

                myPathItems.strokeWidth = 1;

                myPathItems.strokeColor = doc.swatches.getByName("Cutter").color;

            }

        }

    }

}

//Function to check if a named item exists in an array

function existCheck(myArray, name) {

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

        if (myArray.name == name) {

            return true;

        } else {

            return false;

        }

    }

}

The code assumes there is already a layer called "Cutter" (and a swatch called "Cutter"), and that some items on the current layer (that we're duplicating from) are selected.

I would have expected all of the referenced pathItems to have a stroke colour of the swatch "Cutter", and no fill – but only the last item does. The other items have no fill or stroke.

TOPICS
Scripting
645
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Jul 04, 2019 Jul 04, 2019

your script works fine here, win 10, CC2019.

try restarting Illustrator

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 05, 2019 Jul 05, 2019

Thanks. I've restarted Illustrator, but still get the same result. This is using Illustrator CC 2019 23.0.4 on OSX 10.14.5.

The statement affecting the fill is working correctly, affecting all the items in the array, but the statements affecting the stroke only affect the last item, and I'm not sure why.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jul 08, 2019 Jul 08, 2019

Are you sure, all items are pathItems?
If they are compoundPathItems it's not getting them, as you are only selecting pathItems.

Had similar problem while was preparing technical drawings.

App.selection returns collection of pageItems, not only pathItems, so all selected items no matter what type, are copied

But then you catch layer.pathItems and it's not equal with what app.selection might return.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 08, 2019 Jul 08, 2019

essayez d'insérer en fin de boucle ceci,

                   myPathItems.strokeColor = doc.swatches.getByName("Cutter").color;

                   redraw();alert(myPathItems.strokeColor);

            }

afin de voir ce qui se passe?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Jul 11, 2019 Jul 11, 2019

You can upload file to somewhere?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 24, 2019 Jul 24, 2019
LATEST

Thanks for the replies, everyone.

I'm sorry that I've not replied sooner, but I've been away on holiday. Bizarrely, since I got back, the script has now started working correctly, without me having changed either the script or the documents I was testing it on. Again, I've no idea why it wasn't working, or why it now is.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines