Highlighted

Script for Merging Certain Layersets within other LayerSets

Community Beginner ,
Oct 22, 2020

Copy link to clipboard

Copied

Hello!

I'm trying to make a script to simplify psds to pass on to my coworkers. I want to merge subgroups called "Product" and "Shadow." 

Screen Shot 2020-10-22 at 1.39.33 PM.pngScreen Shot 2020-10-22 at 1.40.03 PM.png

I found 2 scripts in this forum that will merge definable Layersets. However, in both cases they only worked on layersets that were not nested inside other layersets. Can anyone help me figure out how to have the script look at all layersets, and merge them if their names are "Product" or "Shadow"?

Here is one script I found:

var sets = new Array();

var match = "Product";

// collect LayerSets of zero level of nesting

for (var i = 0; i < activeDocument.layerSets.length; i++) sets.push(activeDocument.layerSets[i]);

// loop through sets and merge needed (with word "item")

for (var i = 0; i < sets.length; i++) 
    {
    if (sets[i].name.search(new RegExp(match, "i")) >= 0) sets[i].merge();
    }

And the other one was this:

try{

    while(

    lsObj = activeDocument.layerSets["Product"] ) {

lsObj.merge();

}}

catch(e){

; // do nothing

}

I really appriciate the help! You guys are rockstars!

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

Try the following script

var sets = new Array();
var match = "Product";
getLayerSet(activeDocument, sets)
for (var i = 0; i < sets.length; i++) 
{
    if (sets[i].name.search(new RegExp(match, "i")) >= 0)
		sets[i].merge();
}
function getLayerSet(obj, sets)
{
	for (var i = 0; i < obj.layerSets.length; i++)
	{
		sets.push(obj.layerSets[i]);
		getLayerSet(obj.layerSets[i], sets)
	}
}

-Manan

TOPICS
How to, Make It

Views

33

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

Script for Merging Certain Layersets within other LayerSets

Community Beginner ,
Oct 22, 2020

Copy link to clipboard

Copied

Hello!

I'm trying to make a script to simplify psds to pass on to my coworkers. I want to merge subgroups called "Product" and "Shadow." 

Screen Shot 2020-10-22 at 1.39.33 PM.pngScreen Shot 2020-10-22 at 1.40.03 PM.png

I found 2 scripts in this forum that will merge definable Layersets. However, in both cases they only worked on layersets that were not nested inside other layersets. Can anyone help me figure out how to have the script look at all layersets, and merge them if their names are "Product" or "Shadow"?

Here is one script I found:

var sets = new Array();

var match = "Product";

// collect LayerSets of zero level of nesting

for (var i = 0; i < activeDocument.layerSets.length; i++) sets.push(activeDocument.layerSets[i]);

// loop through sets and merge needed (with word "item")

for (var i = 0; i < sets.length; i++) 
    {
    if (sets[i].name.search(new RegExp(match, "i")) >= 0) sets[i].merge();
    }

And the other one was this:

try{

    while(

    lsObj = activeDocument.layerSets["Product"] ) {

lsObj.merge();

}}

catch(e){

; // do nothing

}

I really appriciate the help! You guys are rockstars!

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

Try the following script

var sets = new Array();
var match = "Product";
getLayerSet(activeDocument, sets)
for (var i = 0; i < sets.length; i++) 
{
    if (sets[i].name.search(new RegExp(match, "i")) >= 0)
		sets[i].merge();
}
function getLayerSet(obj, sets)
{
	for (var i = 0; i < obj.layerSets.length; i++)
	{
		sets.push(obj.layerSets[i]);
		getLayerSet(obj.layerSets[i], sets)
	}
}

-Manan

TOPICS
How to, Make It

Views

34

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
Oct 22, 2020 0
Adobe Community Professional ,
Oct 22, 2020

Copy link to clipboard

Copied

Try the following script

var sets = new Array();
var match = "Product";
getLayerSet(activeDocument, sets)
for (var i = 0; i < sets.length; i++) 
{
    if (sets[i].name.search(new RegExp(match, "i")) >= 0)
		sets[i].merge();
}
function getLayerSet(obj, sets)
{
	for (var i = 0; i < obj.layerSets.length; i++)
	{
		sets.push(obj.layerSets[i]);
		getLayerSet(obj.layerSets[i], sets)
	}
}

-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...
Oct 22, 2020 1
Community Beginner ,
Oct 23, 2020

Copy link to clipboard

Copied

Genius! Thank you so much Manan! You are a lifesaver!

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...
Oct 23, 2020 1