• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Create group from layer

Explorer ,
Nov 10, 2020 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,

TOPICS
Actions and scripting , Windows

Views

402

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
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 10, 2020 Nov 10, 2020

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

Votes

Translate

Translate
Adobe
New Here ,
Nov 10, 2020 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.

Votes

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
community guidelines
Explorer ,
Nov 10, 2020 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)

 

Votes

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
community guidelines
New Here ,
Nov 10, 2020 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

Votes

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
community guidelines
Community Expert ,
Nov 10, 2020 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

Votes

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
community guidelines
Explorer ,
Nov 10, 2020 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

Votes

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
community guidelines
Community Expert ,
Nov 10, 2020 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

Votes

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
community guidelines
Explorer ,
Nov 10, 2020 Nov 10, 2020

Copy link to clipboard

Copied

LATEST

Thank you !

Votes

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
community guidelines