Skip to main content
Known Participant
February 28, 2020
Answered

Script “Merge by layerset name”

  • February 28, 2020
  • 2 replies
  • 1369 views

Script “Merge by layerset name”

 

Hi,

in advance ,sorry for my poor English.

I’ll do my best to convey the meaning of the sentence.

 

I’m Japanese photoshop user,and also very big inner to programming.

I need some advice to scripting.

 

I made some very simple script to photoshop .

.JSX contents is like this:

------------------------------------------------

try{

    while(

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

lsObj.merge();

}}

catch(e){

; // do nothing

}

------------------------------------------------

I wanted to merge each layer sets named by each specific keywords before sending to coworker.

The order is like this .[Merge every layer sets only named “item”]

Usually on the graphic design job,I use ton of PSD file and also after working in photoshop ,each PSD file will be so heavy.

that why I made this scrip to lighten file and put into droplet to automate the process.

Fortunately It works.

 

Take up to the main question.Now I’m thinking remodeling little bit this script.

I feel unuseful ,because of the Keyword is to much specific .

Some times on the job,I need to put ohther name like “main item”or”item A,B,C.....etc”.

Every moment in this case,I have to rename keyword before running the script.

So now I’m thinking,trying to find the way to place an order like [Merge every layer sets only  include string“item”]

Ideally,it will merge every layer sets only what I want to merge.

I tryed to use “indexOf()” or maybe other code but it couldn’t work.

I don’t have the basic programming knowledge.

Please give me some advice If someone know how to do this or have an idea about this.

Hope the sentence convey meaning.

thank you for reading to the end.

This topic has been closed for replies.
Correct answer r-bin
var sets = new Array();

var match = "item";

// 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();
    }

2 replies

r-binCorrect answer
Legend
February 29, 2020
var sets = new Array();

var match = "item";

// 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();
    }
kiisoAuthor
Known Participant
February 29, 2020

Hi r-bin .

Thank you very much to your help.

I’ve tried and it works. Genius,this is what I want .

It is difficult to understand whole code yet,but I will try to understand the logic.

I appreciate it very much and have a nice day!

Stephen Marsh
Community Expert
Community Expert
February 29, 2020

Hi kiiso does this help?

 

The script is now interactive, asking for a case sensitive layer set name, so perhaps no longer good for a droplet...

 

 

if (app.documents.length != 0) {
	var setName = prompt("Enter the case sensitive layer set name to merge:", "item");
	function main() {
		try {
			while (
				lsObj = app.activeDocument.layerSets[setName]) {
				lsObj.merge();
			}
		} catch (e) {
			// do nothing
		}
	}
	app.activeDocument.suspendHistory("Named layer set merge", "main()");
} else {
	alert('You must have a document open to use this script!');
}

 

 

kiisoAuthor
Known Participant
February 29, 2020

Hi Stephen .

Thank you very much to your help. Yes you're right ,but it is also interesting ,It comes to mind another ideas to make job easier.

that's very helpful to me.

thank you again and have a nice day!