Skip to main content
Participant
August 18, 2021
Question

Javascript to flatten/merge/rasterize specifically named subgroups

  • August 18, 2021
  • 2 replies
  • 651 views

Hi, I currently have a "flatten.js" javascript that will merge any visible layers in top level groups to a be 1 layer flattened. However, I need it to go through the photoshop file and only flatten nested subgroups with specific prefix's. So for example of the folder structure I have P: Main > BOG: Play > B: Play > then multiple folders= A1: Play, S1: Play, N1: Play. I need those last 3 folders to be flat but stay seperate layers. This is the current script I have, but it is buggy with Photoshop 2021. A new script that works would be very helpful. Thank you

Flatten.js:

 

var doc;

 

if(documents.length > 0) {

doc = activeDocument;

recurse(doc);

alert("Flattening complete!");

}

 

function recurse(psdoc) {

for(var i = 0; i < psdoc.layers.length; i++) {

if(psdoc.layers[i].name == "guides" || psdoc.layers[i].name == "background") {

continue;

} else if(psdoc.layers[i].name.match(/^div:/)) {

if(psdoc.layers[i].layers) {

alert("recurse");

recurse(psdoc.layers[i]);

}

} else {

// flatten

psdoc.layers[i].mergeDown();

}

}

}

 

This topic has been closed for replies.

2 replies

Kukurykus
Legend
August 20, 2021

Post screenshot of your layer(Set)s tree.

johnnyR0yAuthor
Participant
August 21, 2021

So this is a screen shot of the group folders structure I have, the script doesnt necessarily have to go into 5 levels of folders (I can remove the top Folder, but it would need to go into 3 or 4 levels into a group folder. In the screenshot I've manually flattened groups I need the script to flatten.

 

Kukurykus
Legend
August 21, 2021
(function(v){
	var lrs = [].slice.call(v); while(lrs.length) {
		/^[ASN]1/.test((lS = lrs.shift()).name)
		&& lS.layers.length ? lS.merge()
		: callee(lS.layerSets)
	}
})(activeDocument.layerSets)
JJMack
Community Expert
Community Expert
August 18, 2021

You would need to code the script to process your layered file the  he way you want them to be processed. All you file shoulds have a common layer structure that the script is coded to processs,

JJMack
johnnyR0yAuthor
Participant
August 18, 2021

Is there another community forum that I could talk to that could help with coding a new script?

JJMack
Community Expert
Community Expert
August 19, 2021

This forum is one of the best on the web for that. However, to receive good help you will need to post your process design and the code your working on. The script you have does not do what you want to do and what you posted you want to do is less then vague.  There is no actual details about what you want to do or any details about the document you want to process with your automated script procedures.

JJMack