Highlighted

Create group from layer

Community Beginner ,
Nov 10, 2020

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,

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

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

TOPICS
Actions and scripting, Windows

Views

67

Likes

Translate

Translate

Report

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

Create group from layer

Community Beginner ,
Nov 10, 2020

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,

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

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

TOPICS
Actions and scripting, Windows

Views

68

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 10, 2020 0
New Here ,
Nov 10, 2020

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0
Community Beginner ,
Nov 10, 2020

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)

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0
New Here ,
Nov 10, 2020

Copy link to clipboard

Copied

correct. Layer() is not a function, but Layers() is a collection where the ith element can be referenced

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0
Adobe Community Professional ,
Nov 10, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0
Community Beginner ,
Nov 10, 2020

Copy link to clipboard

Copied

@Manan Joshi 

 

Thank you, works well but is reversed ?

 

Before script:

-layerA

-layerB

-layerC

 

After script:

-groupC

--> layerC

-groupB

--> layerB

-groupA

--> layerA

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0
Adobe Community Professional ,
Nov 10, 2020

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 1
Community Beginner ,
Nov 10, 2020

Copy link to clipboard

Copied

Thank you !

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 10, 2020 0