2 Replies Latest reply on Jul 21, 2015 5:04 AM by c.pfaffenbichler

    Find and Remove Layer by Name

    JamesJellings

      I am currently learning JavaScript for Photoshop, but have stumbled on the simplest of requirements - I need to remove a layer from all documents if that layer is present.

       

      For the purpose of the example, the layer is named 'BASE' - and I need to identify whether there is a layer named BASE in the document and then if there is remove it.

       

      Any help would be greatly appreciated.

        • 1. Re: Find and Remove Layer by Name
          Pedro Cortez Marques Level 3

          Hi

           

          You need to be more precise. We layers and groups.

          Do you want to get only the top layers or do you want to delete it from any group or sub-group or sub-sub-group ...?

          • 2. Re: Find and Remove Layer by Name
            c.pfaffenbichler Level 9

            This should get you a list of the matching Layers’ names and IDs.

            You could then use that information to select the Layers with the function "selectLayerByID" for example or … 

            // 2015, use it at your own risk;
            #target "photoshop-70.032"
            if (app.documents.length > 0) {
            var theLayers = findLayersNamed ("BASE");
            alert (theLayers);
            };
            ////////////////////////////////////
            function findLayersNamed (theString) {
            // the file;
            var myDocument = app.activeDocument;
            // get number of layers;
            var ref = new ActionReference();
            ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
            var applicationDesc = executeActionGet(ref);
            var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
            // process the layers;
            var theLayers = new Array;
            for (var m = 0; m <= theNumber; m++) {
            try {
            var ref = new ActionReference();
            ref.putIndex( charIDToTypeID( "Lyr " ), m);
            var layerDesc = executeActionGet(ref);
            var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
            var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
            // if not layer group collect values;
            if (layerSet != "layerSectionEnd" && layerSet != "layerSectionStart" && isBackground != true) {
            var theName = layerDesc.getString(stringIDToTypeID('name'));
            var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
            if (theName == theString) {theLayers.push([theName, theID])}
            };
            }
            catch (e) {};
            };
            return theLayers
            };
            // based on code by mike hale, via paul riggott;
            function selectLayerByID(id,add){
            add = undefined ? add = false:add
            var ref = new ActionReference();
                ref.putIdentifier(charIDToTypeID("Lyr "), id);
                var desc = new ActionDescriptor();
                desc.putReference(charIDToTypeID("null"), ref );
                  if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
                  desc.putBoolean( charIDToTypeID( "MkVs" ), false );
              try{
                executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
            }catch(e){
            alert(e.message);
            }
            };