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: Kukurykus, Tom, xbytor

JeffP
Posts: 2
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: 15
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: 2
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