9 Replies Latest reply on Mar 18, 2018 6:19 PM by Kukurykus

    Rename layer

    Ps-Design Level 1

      Good morning friends! How do I delete the current name of the active layer when replacing with the name entered in the text field of the dialog box? What is missing, where am I going wrong?

       

      #target photoshop 
      var doc = activeDocument; 
      app.preferences.rulerUnits = Units.CM; 
      function layerNamer() { 
      var opts = new Object(); 
      win=new Window("dialog","Replace Layer Name",[0,0,300,100],{resizeable:true,});
      text01=win.add("statictext",[20,30,110,50] ,"Type something:")
      text02=win.add("edittext",[110,30,250,50] ,"")
      but_1=win.add("button",[110,58,197,78],"Replace Layer Name");
      
      but_1.onClick = function() { 
      opts.replace = text02.text;
      processDoc(opts); 
      }
      
      win.center();
      win.show();
      }; 
      if ( app.documents.length > 0) { layerNamer(); }
      function processDoc(opts) { 
           var find; 
           if ( opts.global && !opts.ignore ) { find = new RegExp(opts.find,'gi'); } 
            if ( !opts.global && !opts.ignore ) { find = new RegExp(opts.find,'i'); } 
            var doc = app.activeDocument; 
            recurseLayers(doc.layers); 
            function recurseLayers(layObj) { 
            for ( var i = 0; i < layObj.length; i++ ) { 
                     if ( find.test(layObj[i].name) ) 
                         { 
                               if (layObj[i].kind == LayerKind.TEXT && !opts.txtLay) { continue; } 
                               layObj[i].name = layObj[i].name.replace(find,opts.replace); 
                          } 
                    if ( layObj[i].typename == 'LayerSet' ) 
                          { recurseLayers(layObj[i].layers); } 
                  } 
            } 
        };