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

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
Paul MR

Script to find/replace text in layer names?

Post by Paul MR »

pattesdours

If you want to prefix/suffix the layers name you could do something on the lines of:

Code: Select all//////////////////////////////////////////////////////////////////////////////////////////////
// prefixSuffix true=prefix false = suffix
///////////////////////////////////////////////////////////////////////////////////////////////
function prefixSuffixLayerName(obj,text,prefixSuffix){ 
            if(obj.artLayers.length>0){
            for(var z = 0;z<obj.artLayers.length;z++){
                  var layer = obj.artLayers[z];
                   if(prefixSuffix){
                  layer.name = text +layer.name;
                  }else{
                     layer.name = layer.name + text;
                     }
            if(obj.layerSets.length > 0){
                  for(var l=0;l<obj.layerSets.length;l++){
                 if(prefixSuffix){
                  obj.layerSets[l].name= text + obj.layerSets[l].name;
                  }else{
                     obj.layerSets[l].name= obj.layerSets[l].name + text;
                     }
                        prefixSuffixLayerName(obj.layerSets[l],text,prefixSuffix);
                        }
                  }
            }
         }
}


prefixSuffixLayerName(activeDocument,"Prefix text ",true);
pattesdours

Script to find/replace text in layer names?

Post by pattesdours »

fantastic!
thank you all!
pattesdours

Script to find/replace text in layer names?

Post by pattesdours »

Is there a tutorial somewhere about creating dialog windows with input textfields?

I noticed that Paul's code creates a window that's different depending on the OS. For instance, if I use the Find and Rename script on a windows machine, I see the 'X' at the top of the window so I can close it if change my mind, but there is no such thing on the OSX window. I can still go 'cancel' so it's not a problem, but I'd like to know how to build these right to make sure they're suitable for every environment.

An 'alert' window has all the buttons by default -- can we use an editable textfield with these? Say I want to send a displayed hexadecimal value for a color to clipboard instead of writing it down everytime?

Also, how would you close everything to go back to photoshop when pressing OK on the 'DONE' window? use a return in the code at that moment?
pattesdours

Script to find/replace text in layer names?

Post by pattesdours »

standalone script for add prefix/suffix, with input text dialog :
Code: Select all//
//  add prefix / suffix
//
       
function prefixSuffixLayer(obj, layerNamePrefix, layerNameSuffix)
   {
            if(obj.artLayers.length>0){
            for(var z = 0;z<obj.artLayers.length;z++){
                  var layer = obj.artLayers[z];
                  layer.name = layerNamePrefix + layer.name + layerNameSuffix ;
              }
           }
        }

function prefixSuffixLayerSet(obj, layerNamePrefix, layerNameSuffix){
            if(obj.layerSets.length > 0){
                  for(var l=0;l<obj.layerSets.length;l++){
                  obj.layerSets[l].name= layerNamePrefix + obj.layerSets[l].name + layerNameSuffix;
                  }
              }
     }

function 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:'Add Prefix/Suffix',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:'Prefix:' ,properties:{scrolling:undefined,multiline:undefined}},"+
"statictext1:StaticText{bounds:[10,40,90,57] , text:'Suffix:' ,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:'Add' },"+
"button1:Button{bounds:[122,70,220,100] , text:'Cancel' }} };");

      // set dialog button to execute our function
      win.panel0.button0.onClick = function() {
      prefixSuffixLayer(activeDocument, win.panel0.edittext0.text, win.panel0.edittext1.text);
      prefixSuffixLayerSet(activeDocument, win.panel0.edittext0.text, win.panel0.edittext1.text);
      alert("Add successful");
      }

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