delete all layers including "abc"

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

lee ky
Posts: 6
Joined: Tue Mar 03, 2020 3:59 am

delete all layers including "abc"

Post by lee ky »

Image
I am working on a photoshop sclipt.
in the layer name
I want to delete all layers including "abc".


for (d=1; d < 37 ; ) {
LayerName = "abc"+ d
RemoveLayers(LayerName);
d++;
}

function RemoveLayers(LayerName){

for(var i = 0; i < 15 ; ){


var Layers = documents[0].artLayers;
if (Layers[0].name == LayerName) {
Layers[0].remove();
}

i++;

}


}
Attachments
abc.jpg
abc.jpg (86.2 KiB) Viewed 3287 times
Last edited by lee ky on Tue Mar 03, 2020 4:53 am, edited 4 times in total.

User avatar
Dormeur74
Posts: 32
Joined: Mon Oct 03, 2016 4:56 am

Re: delete all layers including "abc"

Post by Dormeur74 »

This is an old problem we met with dropdown lists with all Visual languages (Visual Basic, Delphi, etc.).
When you want to delete items in a combo or list, you must do it from the last item to the first.
Same thing with layers. You must begin to delete from background to the upper layer. If you begin at 0, it will not work because each time a layer is deleted the order of following layers changes. Hoping you will understand my English.

Try this :

Code: Select all

var docRef = app.activeDocument
var layers = docRef.artLayers;
var nbrLayers = layers.length-1;

while (nbrLayers >-1) {
	var str = layers[nbrLayers].name.search("abc");
	if (str >-1) {
		docRef.activeLayer = layers.getByName(layers[nbrLayers].name);
		docRef.activeLayer.remove();		
	}
	nbrLayers-=1;
}

User avatar
Kukurykus
Posts: 306
Joined: Mon Jul 25, 2016 12:36 pm

Re: delete all layers including "abc"

Post by Kukurykus »

I tried what you said with ExtendScript DropDownList and found I don't have to delete them from last item (but selected one):

Code: Select all

win = new Window('dialog')
lst = win.add('dropdownlist', [0, 0, 100, 20], [1, 2, 3])
btn = win.add('button', undefined, 'Delete an item')
btn.onClick = function() {lst.remove(lst.selection)} win.show()

User avatar
Dormeur74
Posts: 32
Joined: Mon Oct 03, 2016 4:56 am

Re: delete all layers including "abc"

Post by Dormeur74 »

I there is the following items in a dropdown list
- layer dfr
- layer dabc
- layer 123
- layer ght
- layer abc

how do you delete the two items in which the word "abc" is present ? I am not sure to have understood the method.

User avatar
Kukurykus
Posts: 306
Joined: Mon Jul 25, 2016 12:36 pm

Re: delete all layers including "abc"

Post by Kukurykus »

This refers to sole dropdownlist, so without connection to document layers.

lee ky
Posts: 6
Joined: Tue Mar 03, 2020 3:59 am

Re: delete all layers including "abc"

Post by lee ky »

Thank you very much.
Thanks to that, it was solved.

User avatar
Dormeur74
Posts: 32
Joined: Mon Oct 03, 2016 4:56 am

Re: delete all layers including "abc"

Post by Dormeur74 »

Ok, I understand. Thank you.