4 Replies Latest reply on Mar 13, 2015 4:43 PM by WootieCartoons

    name layer same as group

    WootieCartoons Level 1

      Is there a script made that will rename all selected layers the same name as the name of the group they are nested in.  (If not in a group then it skips them.)

        • 1. Re: name layer same as group
          c.pfaffenbichler Level 9

          I doubt such a Photoshop Script exists yet, but it seems feasible.

          Are you comfortable with AM code or do you prefer DOM? (Determining the selected Layers will probably need AM code anyway.)

          • 2. Re: name layer same as group
            WootieCartoons Level 1

            c.pfaffenbichler wrote:

             

            I doubt such a Photoshop Script exists yet, but it seems feasible.

            Are you comfortable with AM code or do you prefer DOM? (Determining the selected Layers will probably need AM code anyway.)

            To quote Willy Wonka:  "You are wrong, sir!  WRONG!!"  -ha.  Well, partially wrong...  Found a script someone has made that renames child layers the same as the parent, and adds a number after it, bonus. BUT if the parent group layer is also selected it does not work.  It would be much less of a headache for us if we could get around that.... anyone?


             

            (function () {
               try {var doc = activeDocument} catch(e) {return}
               var ls = getSelectedLayers();
               if (ls.length < 1) {return}
               k = 1;
               var parentName = "";
               for (i = 0; i<ls.length; i++) {
                  if (ls[i].parent.typename == "LayerSet"){
                     if (parentName != ls[i].parent.name) {k=1; parentName = ls[i].parent.name}
                     ls[i].name = ls[i].parent.name + k
                     k++
                  };
               };
            
            
               function getSelectedLayers() {
                  var A = [];
                  var desc11 = new ActionDescriptor();
                  var ref9 = new ActionReference();
                  ref9.putClass(stringIDToTypeID('layerSection'));
                  desc11.putReference(charIDToTypeID('null'), ref9);
                  var ref10 = new ActionReference();
                  ref10.putEnumerated(charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
                  desc11.putReference(charIDToTypeID('From'), ref10);
                  executeAction(charIDToTypeID('Mk  '), desc11, DialogModes.NO);
                  var gL = activeDocument.activeLayer.layers;
                  for (var i = 0; i < gL.length; i++) {
                     A.push(gL[i]);
                  }
                  executeAction(charIDToTypeID('undo'), undefined, DialogModes.NO);
                  return A;
               }
            })()
            
            • 3. Re: name layer same as group
              c.pfaffenbichler Level 9

              Well, good thing I opined instead of denying …

               

              Looks like one of Paul’s.

               

              Does this work for you? (Based on code by Paul and Mike, I think.)

              // 2015, use it at your own risk;

              #target "photoshop-70.032"

              if (app.documents.length > 0) {

              app.activeDocument.suspendHistory("rename", "main ()");

              };

              ////////////////////////////////////

              function main () {

              var myDocument = app.activeDocument;

              var theLayers = getSelectedLayersIdx();

              // do stuff;

              for (var n = 0; n < theLayers.length; n++) {

              selectLayerByIndex(theLayers[n], false);

              try {

              if (myDocument.activeLayer.typename != "LayerSet" && myDocument.activeLayer.parent != myDocument) {myDocument.activeLayer.name = myDocument.activeLayer.parent.name}

              }

              catch (e) {}

              };

              // reselect layers;

              for (var p = 0; p < theLayers.length; p++) {

                 selectLayerByIndex(theLayers[p], true)

                 };

              };

              // by mike hale, via paul riggott;

              // http://forums.adobe.com/message/1944754#1944754

              function selectLayerByIndex(index,add){

              add = undefined ? add = false:add

              var ref = new ActionReference();

                  ref.putIndex(charIDToTypeID("Lyr "), index);

                  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);

              }

              };

              ////// by paul mr;

              function getSelectedLayersIdx(){

                    var selectedLayers = new Array;

                    var ref = new ActionReference();

                    ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                    var desc = executeActionGet(ref);

                    if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){

                       desc = desc.getList( stringIDToTypeID( 'targetLayers' ));

                        var c = desc.count

                        var selectedLayers = new Array();

                        for(var i=0;i<c;i++){

                          try{

                             activeDocument.backgroundLayer;

                             selectedLayers.push(  desc.getReference( i ).getIndex() );

                          }catch(e){

                             selectedLayers.push(  desc.getReference( i ).getIndex()+1 );

                          }

                        }

                     }else{

                       var ref = new ActionReference();

                       ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));

                       ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

                       try{

                          activeDocument.backgroundLayer;

                          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-1);

                       }catch(e){

                          selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" )));

                       }

                    }

                    return selectedLayers;

              };

              • 4. Re: name layer same as group
                WootieCartoons Level 1

                Heck Yes!  Thanks c.pfaffenbichler, Paul and Mike.