Copy link to clipboard
Copied
Hi,
I try to creat group from layer;
- group keep layer.name,
- then move layer to Group.
function groupLayers(){
for (i=app.activeDocument.layers.length; i>0; i--)
{
var layerName = app.activeDocument.layers;
var groupName = app.activeDocument.layerSets.add();
groupName.name = layerName.name;
layerName.move(groupName, ElementPlacement.INSIDE);
}
}
But i'v got error in line (5):
>> groupName.name = layerName.name;
i've you got an idea ?
regards,
Try the following
function groupLayers(){
var len = app.activeDocument.layers.length
for (var i = len - 1; i >= 0; i--)
{
var layerName = app.activeDocument.layers[len - 1];
if(layerName.isBackgroundLayer)
continue;
var groupName = app.activeDocument.layerSets.add();
groupName.name = layerName.name;
layerName.move(groupName, ElementPlacement.INSIDE);
}
}
-Manan
Copy link to clipboard
Copied
Hi JeremyBep, I think you are simply forgetting the (i) reference for your variable layerName, and so it is referring to a set of layers and not an individual layer. Try changing this line:
var layerName = app.activeDocument.layers;
to this:
var layerName = app.activeDocument.layers(i);
so your code now looks like this:
function groupLayers(){
for (i=app.activeDocument.layers.length; i>0; i--)
{
var layerName = app.activeDocument.layers(i);
var groupName = app.activeDocument.layerSets.add();
groupName.name = layerName.name;
layerName.move(groupName, ElementPlacement.INSIDE);
}
}
however, note that this will create a layer group for each layer and put that layer into that group. no group will have more than one layer in it. if that is what you seek to do, this code should work.
Copy link to clipboard
Copied
Hi, thank for reply,
ha, effectively !
erf, got an error :
Error 24 : Layer() isn't a function
-> var layerName = app.activeDocument.layers(i)
Copy link to clipboard
Copied
correct. Layer() is not a function, but Layers() is a collection where the ith element can be referenced
Copy link to clipboard
Copied
Try the following
function groupLayers(){
for (var i = 0; i < app.activeDocument.layers.length; i++)
{
var layerName = app.activeDocument.layers[i];
if(layerName.isBackgroundLayer)
continue;
var groupName = app.activeDocument.layerSets.add();
groupName.name = layerName.name;
layerName.move(groupName, ElementPlacement.INSIDE);
}
}
-Manan
Copy link to clipboard
Copied
Thank you, works well but is reversed ?
Before script:
-layerA
-layerB
-layerC
After script:
-groupC
--> layerC
-groupB
--> layerB
-groupA
--> layerA
Copy link to clipboard
Copied
Try the following
function groupLayers(){
var len = app.activeDocument.layers.length
for (var i = len - 1; i >= 0; i--)
{
var layerName = app.activeDocument.layers[len - 1];
if(layerName.isBackgroundLayer)
continue;
var groupName = app.activeDocument.layerSets.add();
groupName.name = layerName.name;
layerName.move(groupName, ElementPlacement.INSIDE);
}
}
-Manan
Copy link to clipboard
Copied
Thank you !