Skip to main content
Known Participant
May 15, 2017
Answered

Copy selected and paste to another layer

  • May 15, 2017
  • 2 replies
  • 9422 views

Hello all,

First off, I'm new to this and I've gone through tons of posts concerning this, but to no avail.

Here is what I'm trying to achieve:

  • Create 3 new layers
  • Copy selected artwork from an existing layer, and paste it in the three new layers
  • Paste should be in place

That is the core of my script. I can create the three layers. I can paste what is selected. My issue is I can't paste into the new layers, and paste places the selected items in the center of the page. I don't want to duplicate the layer because there are things on the layer I don't want copied to the other 3 layers.

Another Issue I'd like to resolve is placing the 3 new layers above a layer called Original.

I haven't dug into this either, but how would I go about selecting all the pasted items in the 3 new layers as the last thing the script does?

Illustrator CC 2017 / PC

Thanks all.

//My added Layers;
sourceDoc = activeDocument;

app.copy();

targetLayer = sourceDoc.layers.add();
targetLayer.name = "Layer1";

redraw();

app.paste();

targetLayer = sourceDoc.layers.add();

targetLayer.name = "Layer2";

redraw();

app.paste();

targetLayer = sourceDoc.layers.add();

targetLayer.name = "Layer3";

redraw();

app.paste();

This topic has been closed for replies.
Correct answer Disposition_Dev

So that's where things get a little bit stickier since we're creating the layers inside a group(typo.. sorry) loop. But here's a simple way to set that up if you know ahead of time what you want the layers to be called. Now it's perfectly plausible to abstract this process so that you could determine the names at runtime via some kind of dialog or by reading an existing file etc, but that's getting a little bit into the weeds and is kind of off topic. (feel free to open a new discussion on that topic)

But for the simple purposes of this specific snippet, we can use an array of layer names to use from inside the loop.

function test() 

    var docRef = app.activeDocument; 

    var layers = docRef.layers; 

    //how many new layers do you want? 

    var numLayers = 3; 

    //what are the names of the layers

    //be careful that the number of names you put in here matches the

    //numLayers variable above. If numLayers = 3, but you only put 2 layer

    //names in this array, you'll get an error.

    var layerNames = ["Alpha", "Beta", "Delta"];

 

    var sel = docRef.selection; 

 

    //save the original layer to a variable so we can put it at the bottom of the list 

    var originalLayer = layers[0]; 

 

    //create new layers 

    for(var x=0;x<numLayers;x++) 

    { 

        var newLay = layers.add();

        newLay.name = layerNames;

        //now copy the selection into the newly created layer 

        for(var y = sel.length-1;y >-1; y--) 

        { 

            var thisItem = sel.duplicate(newLay); 

        } 

    } 

 

    originalLayer.zOrder(ZOrderMethod.SENDTOBACK); 

test();

2 replies

pixxxelschubser
Community Expert
Community Expert
May 15, 2017

Hi michaelk97081953,

here is another way

// copySelectionToThreeNewLayers.jsx

alert("At first: disable Remember the Layers in the options of your layers palette"); // if so, you can comment out this alert

///////////////////////////////////////////////////////////////////////////

var numberOfNewLayers = 3;

///////////////////////////////////////////////////////////////////////////

// required: an opened document with one selected item as minimum

var aDoc = app.activeDocument;

var aSel = aDoc.selection;

app.executeMenuCommand ('copy');

for (i = 0; i < numberOfNewLayers; i++) {

    pasteInLayer ();

    }

function pasteInLayer () {

    var aLay = aDoc.layers.add();

    app.executeMenuCommand ('pasteInPlace');

    return;

}

Have fun

Disposition_Dev
Legend
May 15, 2017

That's a nice concise way to do it, pixxxel.

For future reference, app.pasteRemembersLayers is a settable property. So if you're doing something that requires a certain setting, you can use:

app.pasteRemembersLayers = true/false;

pixxxelschubser
Community Expert
Community Expert
May 15, 2017

Hi williamadowling,

I know the property as well.

But in this simple snippet I don't use error management like

if ( documents.length …

if ( selection.length …

if ( app.pasteRemembersLayers …

And thats why the alert was the "best" solution in this case for me. Because of: no requirement to restore the original state in my script snippet.

But thanks.

And I'm so sorry. But Jive (the forum software) is buggy for me. Impossible for me to edit my own answers or mark answers as helpful or, or, or …

Disposition_Dev
Legend
May 15, 2017

*EDIT*

I forgot to explain.

First of all, there's a differentiation between app.paste(); and the "pasteInPlace" functionality you're talking about. app.paste() just pastes the copied artwork in the center of the window (not necessarily the artboard). To my knowledge, there is no "app.pasteInPlace();" method. If you're using CS6+, you can use:

app.executeMenuCommand("pasteInPlace");

but for performance reasons, i tend to avoid the executeMenuCommand() method when possible.

Below i've illustrated the usage of the object.duplicate() method and passed in the argument "newLay" which represents the destination layer. the duplicate() method does not move the art, so by default, the art is duplicated "in place".

Hope this works well for you. Good luck. =)

function test()

{

    var docRef = app.activeDocument;

    var layers = docRef.layers;

    //how many new layers do you want?

    var numLayers = 3;

    var sel = docRef.selection;

    //save the original layer to a variable so we can put it at the bottom of the list

    var originalLayer = layers[0];

    //create new layers

    for(var x=0;x<numLayers;x++)

    {

        var newLay = layers.add();

        //now copy the selection into the newly created layer

        for(var y = sel.length-1;y >-1; y--)

        {

            var thisItem = sel.duplicate(newLay);

        }

    }

    originalLayer.zOrder(ZOrderMethod.SENDTOBACK);

}

test();

Known Participant
May 15, 2017

Wow William, that was fast. Thank you so very much. Looking at your script, I don't think I would have gone that way. Yours looks clean and easy. I tested it, and it does what I want it to do. How can I name the layers something specific like Alpha, Beta and Delta? I can't  really just rename them in the script because Layer11 might change to 12 or 13. I use 10 layers and Original is one of the layers, but sometimes it varies, usually one or two more.

I didn't mention this in my post but after this script, I will run another script to change numbers that are in a text file created by an Excel macro. Basically I have callouts to parts, numbered 1 to whatever, the second scripts changes those numbers I copied to the three new layers to other numbers like part numbers, customer numbers, and part ordering numbers. It's a time consuming manual process.

Hope this wasn't to confusing, I kinda jumped round.

MichaelK ¯\_(ツ)_/¯
Disposition_Dev
Disposition_DevCorrect answer
Legend
May 15, 2017

So that's where things get a little bit stickier since we're creating the layers inside a group(typo.. sorry) loop. But here's a simple way to set that up if you know ahead of time what you want the layers to be called. Now it's perfectly plausible to abstract this process so that you could determine the names at runtime via some kind of dialog or by reading an existing file etc, but that's getting a little bit into the weeds and is kind of off topic. (feel free to open a new discussion on that topic)

But for the simple purposes of this specific snippet, we can use an array of layer names to use from inside the loop.

function test() 

    var docRef = app.activeDocument; 

    var layers = docRef.layers; 

    //how many new layers do you want? 

    var numLayers = 3; 

    //what are the names of the layers

    //be careful that the number of names you put in here matches the

    //numLayers variable above. If numLayers = 3, but you only put 2 layer

    //names in this array, you'll get an error.

    var layerNames = ["Alpha", "Beta", "Delta"];

 

    var sel = docRef.selection; 

 

    //save the original layer to a variable so we can put it at the bottom of the list 

    var originalLayer = layers[0]; 

 

    //create new layers 

    for(var x=0;x<numLayers;x++) 

    { 

        var newLay = layers.add();

        newLay.name = layerNames;

        //now copy the selection into the newly created layer 

        for(var y = sel.length-1;y >-1; y--) 

        { 

            var thisItem = sel.duplicate(newLay); 

        } 

    } 

 

    originalLayer.zOrder(ZOrderMethod.SENDTOBACK); 

test();