Skip to main content
Geppetto Luis
Legend
May 24, 2021
Answered

Delete level based on first name

  • May 24, 2021
  • 3 replies
  • 1275 views

Good morning
I would like to delete the level with initial test name
however the level has the name test v1
v1 is not to be taken into account
must delete the level based on the test name only.

 

This is what the level looks like

 

This is what I have now

app.activeDocument.layers.getByName("test").remove();

This topic has been closed for replies.
Correct answer Stephen Marsh

 

I modified another script into something that works for me, perhaps it will work for you too:

 

/*
Delete level based on first name
https://community.adobe.com/t5/photoshop/delete-level-based-on-first-name/td-p/12062329
*/

#target photoshop

function main() {
    // Call the function to process layers/layerSets
    processLayersAndSets(app.activeDocument);

    function processLayersAndSets(target) {
        // Loop through all layers
        for (var l = target.artLayers.length - 1; 0 <= l; l--) {
            // Call the removeLayer function
            removeLayer(target.artLayers[l]);
        }
        // Loop through all layer groups/sets
        for (var s = target.layerSets.length - 1; 0 <= s; s--) {
            // Call the removeLayer function
            processLayersAndSets(target.layerSets[s]);
        }
        // Remove non-background layers beginning with the word "test", case insensitive
        function removeLayer(layer) {
            if (!layer.isBackgroundLayer & layer.name.match(/^test\b/i) !== null) layer.remove();
        }
    }
}
app.activeDocument.suspendHistory("Run script", "main()");

 

 

 

3 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
May 24, 2021

 

I modified another script into something that works for me, perhaps it will work for you too:

 

/*
Delete level based on first name
https://community.adobe.com/t5/photoshop/delete-level-based-on-first-name/td-p/12062329
*/

#target photoshop

function main() {
    // Call the function to process layers/layerSets
    processLayersAndSets(app.activeDocument);

    function processLayersAndSets(target) {
        // Loop through all layers
        for (var l = target.artLayers.length - 1; 0 <= l; l--) {
            // Call the removeLayer function
            removeLayer(target.artLayers[l]);
        }
        // Loop through all layer groups/sets
        for (var s = target.layerSets.length - 1; 0 <= s; s--) {
            // Call the removeLayer function
            processLayersAndSets(target.layerSets[s]);
        }
        // Remove non-background layers beginning with the word "test", case insensitive
        function removeLayer(layer) {
            if (!layer.isBackgroundLayer & layer.name.match(/^test\b/i) !== null) layer.remove();
        }
    }
}
app.activeDocument.suspendHistory("Run script", "main()");

 

 

 

Geppetto Luis
Legend
May 25, 2021

Thank you all

c.pfaffenbichler
Community Expert
Community Expert
May 24, 2021
Kukurykus
Legend
May 24, 2021

Make a loop to take into account all 'levels' that starts with 'test' word.

Geppetto Luis
Legend
May 24, 2021

Kukurykus
You are always kind in wanting to help me
I honestly don't know where to start
a small string of code would be useful

thanks again for your help

Kukurykus
Legend
May 24, 2021

Are you serious? After so many years getting experience and samples from this forum and using much more advanced codes you are asking for one of most simplest thing to do, so making loop over layers? There are plenty threads showing how to do it - browse them.