Skip to main content
johndys
Known Participant
December 3, 2018
Answered

Copy contents of found layers into a new layer

  • December 3, 2018
  • 2 replies
  • 1525 views

down voteI'm looking for an illustrator script that will do the following.

  • Find layers that contain "Art1", "Art2", "Art3" and copy its' content to a new layer named "GroupedArt". There will be times when "Art2" or "Art3" does NOT exist and the elements copied must be in the same location/place.
  • Select all the contents in this new "GroupedArt" layer and remove all fill and set stroke to 100% Cyan.

That's it... seems so simple but I'm not able accomplish this. I've searched all day for sample scripts but zero luck. Any help is greatly appreciated. Thank you in advance.

This topic has been closed for replies.
Correct answer Disposition_Dev

Hello William,

One day, I'd like to be in your position where I'm giving back to the community that gave to you ;-) There is a link to the sample file you are requesting.

Also, i ran the script both on my mac & pc, and when I use duplicate method, illustrator freezes.

Dropbox - SampleArt.ai

Thanks a bunch for your help.


well fooey! i knew that while loop would come back to bite me in the rear... I originally had a for loop working backwards to move all of the artwork to the GroupedArt layer, but for some reason it was not moving everything.. So i took the easy route and just made it keep looping until the number of items on the source layer (Art1 for example) was zero. This works great if you're MOVING art.. but if you're duplicating artwork, then the number of items on the source layer will NEVER be zero...

By changing from moveToBeginning() to duplicate(), we created an infinite loop.... my shame shall live on forever, just like that while loop.

give this one a try. I also added some support for unlocking/relocking unhiding/rehiding layers so that you won't get an error if it tries to manipulate a locked/hidden layer, but it will also re-set everything back to the way it was after the art is duplicated. there's probably a better way to do it, but this works just fine. Hope this helps.

function test()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var swatches = docRef.swatches;

    var searchTerms = ["Art1", "Art2", "Art3"];

    var stLen = searchTerms.length;

    var groupedArtLayer = layers.add();

    groupedArtLayer.name = "GroupedArt";

    var cyanStrokeColor = new CMYKColor();

    cyanStrokeColor.cyan = 100;

    cyanStrokeColor.magenta = 0;

    cyanStrokeColor.yellow = 0;

    cyanStrokeColor.black = 0;

    var cyanSwatch = swatches.add();

    cyanSwatch.name = "Cyan";

    cyanSwatch.color = cyanStrokeColor;

    function copyArtToLayer(srcLay, destLay)

    {

        var reLock = srcLay.locked ? true : false;

        var reHide = !srcLay.visible ? true : false;

        srcLay.locked = false;

        srcLay.visible = true;

        var item;

        for(var x = srcLay.pageItems.length -1; x>=0;x--)

        {

            item = srcLay.pageItems;

            item.duplicate(destLay);

        }

        srcLay.locked = reLock ? true : false;

        srcLay.visible = reHide ? false : true;

    }

    var curLay, curLayName, curTerm;

    for (var x = layers.length - 1; x > 0; x--)

    {

        curLay = layers;

        curName = curLay.name;

        for (var y = 0; y < stLen; y++)

        {

            curTerm = searchTerms;

            if (curName === curTerm)

            {

                copyArtToLayer(curLay, groupedArtLayer);

            }

        }

    }

    docRef.selection = null;

    groupedArtLayer.hasSelectedArtwork = true;

    docRef.defaultFillColor = swatches["[None]"].color;

    docRef.defaultStrokeColor = swatches["Cyan"].color;

}

test();

2 replies

Disposition_Dev
Legend
December 4, 2018

woops. sorry, my reading comprehension is evidently pretty low.. i forgot about the no fill, cyan stroke part.

try this.

function test()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var swatches = docRef.swatches;

    var searchTerms = ["Art1", "Art2", "Art3"];

    var stLen = searchTerms.length;

    var groupedArtLayer = layers.add();

    groupedArtLayer.name = "GroupedArt";

    var cyanStrokeColor = new CMYKColor();

    cyanStrokeColor.cyan = 100;

    cyanStrokeColor.magenta = 0;

    cyanStrokeColor.yellow = 0;

    cyanStrokeColor.black = 0;

    var cyanSwatch = swatches.add();

    cyanSwatch.name = "Cyan";

    cyanSwatch.color = cyanStrokeColor;

    function copyArtToLayer(srcLay, destLay)

    {

        var item;

        while (srcLay.pageItems.length)

        { 

            item = srcLay.pageItems[srcLay.pageItems.length - 1];

            item.filled = false;

            // item.strokeColor = cyanStrokeColor;

            item.moveToBeginning(destLay);

        }

    }

    var curLay, curLayName, curTerm;

    for (var x = layers.length - 1; x > 0; x--)

    {

        curLay = layers;

        curName = curLay.name;

        for (var y = 0; y < stLen; y++)

        {

            curTerm = searchTerms;

            if (curName === curTerm)

            {

                copyArtToLayer(curLay, groupedArtLayer);

            }

        }

    }

    docRef.selection = null;

    groupedArtLayer.hasSelectedArtwork = true;

    docRef.defaultFillColor = swatches["[None]"].color;

    docRef.defaultStrokeColor = swatches["Cyan"].color;

}

test();

Stephen Marsh
Community Expert
Community Expert
December 4, 2018

Hi William, this appears to move the original content to the new layer leaving the original layers intact but empty, not create a copy with the original layers left intact? P.S. What if the search layers are locked or objects are locked?

Disposition_Dev
Legend
December 4, 2018

schwoops. again with the reading comprehension.

swapping out the "moveToBeginning()" commands with "duplicate()" should fix that.

and you're right about things being locked and/or hidden. i was not very conscious of making this script bulletproof since i was essentially taking a break to just help out a little. there are a large number of improvements that can be made, for sure.

Disposition_Dev
Legend
December 4, 2018

Hello there, friend.

Give this a try. feel free to add or remove items to the "searchTerms" array as needed. you could add as many potential layer names as needed.

function test()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    var searchTerms = ["Art1", "Art2", "Art3"];

    var stLen = searchTerms.length;

    var groupedArtLayer = layers.add();

    groupedArtLayer.name = "GroupedArt";

    function copyArtToLayer(srcLay, destLay)

    {

        while (srcLay.pageItems.length)

        {

            srcLay.pageItems[srcLay.pageItems.length - 1].moveToBeginning(destLay);

        }

    }

    var curLay, curLayName, curTerm;

    for (var x = layers.length - 1; x > 0; x--)

    {

        curLay = layers;

        curName = curLay.name;

        for (var y = 0; y < stLen; y++)

        {

            curTerm = searchTerms;

            if (curName === curTerm)

            {

                copyArtToLayer(curLay, groupedArtLayer);

            }

            docRef.selection = null;

        }

    }

}

test();