Skip to main content
Inspiring
Jun 16, 2020 11:19 AM UTC
Answered

create group from existing layers

I have 2 layers that I wish to place in a new group called DATA, they have just been linked by the follwing code and so I can learn a bit more is it better to create the group first and add layers to it or does that not matter?

activeDocument.layers['INFO'].link(activeDocument.layers['ICONS']);
This topic is closed to new replies. Start a new post to keep the conversation going.
Kukurykus
KukurykusCorrect answer
Legend
Jun 16, 2020 6:49 PM UTC

No, my bad, something else I need to learn how to do

Mike


sTT =stringIDToTypeID, dsc = new ActionDescriptor(), ref = new ActionReference()
for(i = 0; i < (a = ['INFO', 'ICONS']).length;) ref.putName(sTT('layer'), a[i++])
dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc)

10 replies

Kukurykus
Legend
Jun 16, 2020 4:04 PM UTC

Select both layers (to be grouped) and use this code:

 

 sTT = stringIDToTypeID;
 
(ref1 = new ActionReference()).putClass(sTT('layerSection'));
(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref1);
(ref2 = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT('targetEnum'))
dsc1.putReference(sTT('from'), ref2); (dsc2 = new ActionDescriptor()).putString(sTT('name'), 'DATA')
dsc1.putObject(sTT('using'), sTT('layerSection'), dsc2), executeAction(sTT('make'), dsc1)

 

Translate
Inspiring
Jun 16, 2020 6:25 PM UTC

This created the new group above existing layers but only put the top layer in the group.

Mike

Translate
Kukurykus
Legend
Jun 16, 2020 6:27 PM UTC

Did you select both layers like I said?

Translate
c.pfaffenbichler
Community Expert
Community Expert
Jun 16, 2020 11:41 AM UTC

Do you want to use DOM-code or AM-code? 

 

Referencing Layers by name seems a bit risky by the way. 

If you created the Layers withing the Script you may want to define them as a var right away. 

Translate
Inspiring
Jun 16, 2020 11:58 AM UTC

In all honesty I do not know the difference between the 2 types of code

The layer names will always be the same as they are created in the script

I have just found a way to create a Group (but this may not be the best method obviously)

 

 

	var docRef = app.activeDocument;
	var newGroup = docRef.layerSets.add();
	newGroup.name = "DATA";

 

so if I add that early enough I can use the following to add the layers to the group using after each new layer

 

activeDocument.activeLayer.move(docRef.layerSets["DATA"], ElementPlacement.INSIDE);

 


but when I try to run this I get an error

 

	activeDocument.layers['INFO'].link(activeDocument.layers['ICONS']);

 



 

Translate
Kukurykus
Legend
Jun 16, 2020 4:09 PM UTC
lrs = (aD = activeDocument).layers
inf = lrs['INFO'], icns = lrs['ICONS']

//	use here code to move layers to layerSet

inf.link(icns)
Translate