Sg2.9
duplicating layers
Recent RSS
duplicating layers
From:  chris h
Date:  25. April 2013, 07:24

Hello,

I'm attempting to write a script that will cycle through a document's layers and for each layer in the document that contains selected items:
*copy the active layer's children to a duplicate layer
*name that duplicate layer "[layer name] copy"
*move the duplicate layer below the original layer
*turn off / hide the original layers that were copied from.

I think I'm missing something obvious in my script, anyone care to take a look?

thanks,

  • chris
Re: duplicating layers
Date:  25. April 2013, 12:14

Hi Chris,

you need to test each selected item what layer it belongs to in your first for-loop. Something like this psuedocode:

get layers array
get selected items array
while selected items
{
   for every layer in layers array
   {
      if item belong to layer
      {
         if not layer copy
         }
             make layer copy 
         }
         copy item to layer copy
         splice item from selected items array
      }
   }
}
move layer copy below original layer
name layer copy
hide original layer

If I find the time tonight I could write it in JS and post it here. That is, if no one beats me to it.

Hope it helps

Re: duplicating layers
Date:  25. April 2013, 13:11

Well, actually, the items while loop should be inside the layers for loop, but maybe you get the idea.

I'll correct this later as it is kinda hard to write scripts on an iPhone :)

Re: duplicating layers
From:  chris h
Date:  25. April 2013, 17:03

Ah, thanks! I thought I would have to have a loop within a loop to accomplish this. I only have CS6 on my home machine so will have to wait till I'm at work before trying to test this.

Re: duplicating layers
From:  chris h
Date:  25. April 2013, 18:12

so perhaps something like this?

/* second attempt */

// get the document layers array
var layers = document.layers;

// test to make sure layers is returning the document layers array
for (i=0; i<layer.length; i++){
print(layer[i].name);
}

// get the selected items in the document
var selItems = document.getItems({
selected: true
});

// make the item's layer the active layer in the document
selItem.layer.activate()

// while items are selected
while selItems.selected = true {

// cycle through the document layers
for (layer in document.layers) {

// if the item is in the active layer
if (selItem.layer == document.activeLayer){

//if the item is not in the copied layer
if (selItem.layer != layerCopy){

//make a duplicate layer
layerCopy = new Layer();

//copy the original layer name to the duplicate
layerCopy.name = selItem.layer.name + 'COPY';
}

// clone the original layer's children
layerClone = selItem.layer.children.clone();

// append the cloned children to the new layer
layerCopy.children = layerClone.children;

//remove cloned layer
layerClone.remove();

//unselect original layer's items
selItems.selected = false;

}
}
}

var newLayer = layerCopy;
var oldLayer = selItem.layer;

// move the newLayer below the oldlayer
newLayer.moveBelow(oldLayer);

// make the old layer hidden
oldLayer.visible = false;

Re: duplicating layers
Date:  25. April 2013, 21:02

Hi Chris,

this prooved to be a bigger challenge than I initially thought. The problem is that when I add a new layer the document.layers variable changes, very annoying indeed.
It's been a while since I wrote any scripts so I'm not entirely back in the saddle again.
I'll think about it for some hours and maybe I'll get back to you tomorrow, hopefully with a smarter solution than the one I have at the moment...

Re: duplicating layers
From:  Chris H
Date:  25. April 2013, 21:39

Ah, that makes sense. I also sent you an email Hakan. Thanks again for looking into this, I appreciate the help!

  • Chris
Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Stroke <-> Fill 09.01.08