Delete duplicate layer names

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

JeffP
Posts: 3
Joined: Tue Sep 15, 2020 5:52 pm

Delete duplicate layer names

Post by JeffP »

Hello
I'm in desperate need of a script which will delete duplicate layers of the same name, ( non-name specific), leaving only one layer of that name remaining.

As an example, from this:
Layer1
Layer1
Layer1

to this:
Layer1

Any ideas?
Thanks!
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Delete duplicate layer names

Post by jaydoubleyou80 »

I think this should do it, but if you have grouped layers that would need some tweaking.

Code: Select all

var myDoc = app.activeDocument;
try {
	for(n=0;n<myDoc.layers.length;n++){
		for (i=1; i < myDoc.layers.length; i++){
			if(myDoc.layers[n].name === myDoc.layers[i].name){
				myDoc.layers[i].remove();
				}
			}
		}
	}
catch (err) {}
JeffP
Posts: 3
Joined: Tue Sep 15, 2020 5:52 pm

Re: Delete duplicate layer names

Post by JeffP »

Thanks for the reply, jaydoubleyou80

Tried running your script, but it's yielding unusual results.
With a document of standard layers, (No groups) whereby the first two layers; Hello, and Goodbye are Text layers
Before running the script my document contained the following layer names:

Hello
Goodbye
Layer_2
Layer_2
This is Layer A
This is Layer B
TTS_Layer
TTS_Comp

After running the script the results were:

Hello
Layer_2
This is Layer B
TTS_Comp

I should note, that I'm using Photoshop CS6.

Any Suggestions?

Thanks!
Jeff
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Delete duplicate layer names

Post by jaydoubleyou80 »

You made me realize I wasn't following my code through the process in my mind. As the script was iterating through, it was deleting layers as it was going. But that was modifying the index, so a layer could very easily be skipped. I am positive there is a better way to do this, but in order to account for layer indexes changing, I had to stop deleting right away. Instead it changes the name of the layer that matches another layer name to "Delete Me."

Then I had the same problem going through and deleting those layers, so I set up a function to use the original layer count as a number of passes through the layers. That means that if you have a document with a few hundred layers, the function is going to run through them looking for layers with the name Delete Me that many times. Depending on your computer, and your files, that could take some time.

I think you could simplify it by first counting the number of layers that have that name and using that as the starting value of X, but I have to stop working on this for now.

I hope it helps!

Code: Select all

var myDoc=app.activeDocument;
var allLayers=[];
for (w=0; w<myDoc.layers.length; w++){
	allLayers.push(myDoc.layers[w]);
}
var layerLength=myDoc.layers.length;
for (r=0; r<layerLength; r++){
	for (s=r + 1; s<layerLength; s++){
		if (allLayers[r].name === allLayers[s].name){
			allLayers[s].name = "Delete Me";
		}
	}
}
deleteLayers();
function deleteLayers(){
	var x=layerLength;
	while (x>0){
		for (t = 0; t < layerLength; t++){
			try{
				if (myDoc.layers[t].name === "Delete Me"){
					myDoc.layers[t].remove();
					}
				} catch (err){}
			}
		x--;
		}
	}
JeffP
Posts: 3
Joined: Tue Sep 15, 2020 5:52 pm

Re: Delete duplicate layer names

Post by JeffP »

This should work for me..!

Thank you very much, jaydoubleyou80

Cheers!
Jeff
User avatar
jaydoubleyou80
Posts: 20
Joined: Mon Oct 17, 2016 1:41 pm
Location: USA

Re: Delete duplicate layer names

Post by jaydoubleyou80 »

Glad I could help!