Script to find/replace text in layer names?

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: Tom, Kukurykus

Sequitur

Script to find/replace text in layer names?

Post by Sequitur »

I'm looking for a script to perform a standard text "find and replace" of group names and layer names within photoshop... a way of error-checking documents that have many hundreds of layers that will need to conform to a particular naming convention.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Script to find/replace text in layer names?

Post by Paul MR »

Here's a start, might be a bit slow though..

Code: Select allfunction findReplaceLayerName(obj,Find,Replace){
var regFind = new RegExp(Find,"gi");
        if(obj.artLayers.length>0){
        for(var z = 0;z<obj.artLayers.length;z++){
                var layer = obj.artLayers[z];
                layer.name = layer.name.replace(regFind,Replace);
        }
        if(obj.layerSets.length > 0){
                for(var l=0;l<obj.layerSets.length;l++){
                        findReplaceLayerName(obj.layerSets[l],Find,Replace);
                        }
                }
        }
   }
findReplaceLayerName(activeDocument,"Text","NewText");
Sequitur

Script to find/replace text in layer names?

Post by Sequitur »

"Here's a start, might be a bit slow though.. "

Wow, thanks- that's a great start!
Sequitur

Script to find/replace text in layer names?

Post by Sequitur »

If I have a list of separate characters or strings of text that I want to replace with a single character or string, I'm wondering how best to implement that?

For instance, if I want to replace all uses of #, @, &, !, etc... with an underscore or other such character.


I see duplicating the command line

"findReplaceLayerName(activeDocument,"Text","NewText"); "

doesn't work... still very new to this stuff!
pattesdours

Script to find/replace text in layer names?

Post by pattesdours »

Unless I'm mistaken, the function uses regular expressions. Does anyone have photoshop javascript experience in the matter? Can we use reg-exp inside the parameter parenthesis instead of using quotes? Also, would it be a lot of work to have a dialogue window pop up so we could enter the two separate strings for 'text' and 'new text' with default values to return the original name ($1 + "suffix") or something?

(picked-up on my enthousiasm yet? I've been wanting to do something like this for a while )
xbytor

Script to find/replace text in layer names?

Post by xbytor »

You would have to do this in two passes. One to do the simple text replacement, and a second to remove special characters.

Code: Select allfindReplaceLayerName(activeDocument, "Text", "NewText");
findReplaceLayerName(activeDocument, /\#|@|&|!/g, '_');


-X
pattesdours

Script to find/replace text in layer names?

Post by pattesdours »

Nice... and what would the syntax be if you were to do this ?

'entire original layername'
(becomes)
'prefix' + 'entire original layername' + 'suffix'

I'm thinking $1 $2 and so on would return words if the layer name contains spaces. How would I get the whole thing without using layer.name?
Patrick

Script to find/replace text in layer names?

Post by Patrick »

pattesdours wrote:Can we use reg-exp inside the parameter parenthesis instead of using quotes?

That function Paul posted is just using a regex string, so you can just put in a regex that meets all your needs into that function. For example you should be able to just use "[#@&!]" without quotes as your find string to replace all of those in the same pass.

I took what Paul posted and added a dialog for it.

Code: Select allfunction main()
{
   try
   {
      
      // Paul's function to loop through document
      function findReplaceLayerName(obj,Find,Replace){
      var regFind = new RegExp(Find,"gi");
            if(obj.artLayers.length>0){
            for(var z = 0;z<obj.artLayers.length;z++){
                  var layer = obj.artLayers[z];
                  layer.name = layer.name.replace(regFind,Replace);
            }
            if(obj.layerSets.length > 0){
                  for(var l=0;l<obj.layerSets.length;l++){
                        findReplaceLayerName(obj.layerSets[l],Find,Replace);
                        }
                  }
            }
         }

      // build dialog box
      var win = new Window("dialog{text:':)',bounds:[100,100,350,240],\
            panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},\
               statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},\
               statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},\
               edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
               edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},\
               button0:Button{bounds:[10,70,220,100] , text:'Go!' }\
            }\
      };");

      // set dialog button to execute our function
      win.panel0.button0.onClick = function() {
         findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
         alert("DONE!");
      }

      // show user the dialog
      win.center();
      win.show();

   } catch(e) {
      // if the script fails, give the user a reason
      alert("Script failed with the following error: \n\n"+ e);
   }
}

// run script
main();

Patrick
Paul MR

Script to find/replace text in layer names?

Post by Paul MR »

Thanks Patrick and Xbytor, Just modified the script slightly Patrick it should now rename layerset names as well as layer names.

Code: Select allfunction main()
{
   try
   {
       
      function findReplaceLayerName(obj,Find,Replace){
      var regFind = new RegExp(Find,"gi");
            if(obj.artLayers.length>0){
            for(var z = 0;z<obj.artLayers.length;z++){
                  var layer = obj.artLayers[z];
                  layer.name = layer.name.replace(regFind,Replace);
            }
            if(obj.layerSets.length > 0){
                  for(var l=0;l<obj.layerSets.length;l++){
                  var lname = obj.layerSets[l].name.replace(regFind,Replace);
                  obj.layerSets[l].name=lname;
                        findReplaceLayerName(obj.layerSets[l],Find,Replace);
                        }
                  }
            }
         }

      // build dialog box
      var win = new Window("dialog{text:':)))',bounds:[100,100,350,240],"+
"panel0:Panel{bounds:[10,10,240,130] , text:'Find & Replace' ,properties:{borderStyle:'etched',su1PanelCoordinates:true},"+
"statictext0:StaticText{bounds:[10,20,90,37] , text:'Find:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"statictext1:StaticText{bounds:[10,40,90,57] , text:'Replacement:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"edittext0:EditText{bounds:[100,16,220,36] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"edittext1:EditText{bounds:[100,40,220,60] , text:'' ,properties:{multiline:false,noecho:false,readonly:false}},"+
"button0:Button{bounds:[10,70,120,100] , text:'Go!' },"+
"button1:Button{bounds:[122,70,220,100] , text:'Cancel' }} };");

      // set dialog button to execute our function
      win.panel0.button0.onClick = function() {
         findReplaceLayerName(activeDocument,win.panel0.edittext0.text,win.panel0.edittext1.text);
         alert("DONE!");
      }

      // show user the dialog
      win.center();
      win.show();

   } catch(e) {
      // if the script fails, give the user a reason
      alert("Script failed with the following error: \n\n"+ e);
   }
}

// run script
main();
xbytor

Script to find/replace text in layer names?

Post by xbytor »

FWIW, this should should remove " copy" layer name extensions as well as any associated sequence numbers.

Code: Select allfindReplaceLayerName(activeDocument, " copy( \\d+)?$", '')

-X