8 Replies Latest reply on May 10, 2011 7:07 AM by Muppet Mark-QAl63s

    Case insensitive replace

    Ghoulfool Level 1

      I'm working on a script that does a text search and replace in layer names, but it could be improved with the addition of case insensivity

       

      function processLayers (srcDoc) {

       

      for (var i = 0; i < numLayers; i++)
      {
      var thisLayer = srcDoc.artLayers[i].name;

      if (thisLayer.search(mySearch) != -1)
      {
         tempName = app.activeDocument.artLayers[i].name
         app.activeDocument.artLayers[i].name = tempName.replace(mySearch,myReplace)
      }
      }

      } //function close

       

      I know the javascript method is to add slash i for case insensitive but I don't know how that transfers to the Photoshop DOM. Can anyone kindly help me out. Cheers

        • 1. Re: Case insensitive replace
          Muppet Mark-QAl63s Level 4

          Im not sure I understand what you want here… Could you not just pass your 'mySearch' to the reg exp constructor and have that include case or ignore case…

          • 2. Re: Case insensitive replace
            Michael L Hale Level 5

            You can either use a RegExp as Mark suggested or force the case to what ever case your replace string is using.

             

            tempName = app.activeDocument.artLayers[0].name.toLowerCase();

            • 3. Re: Case insensitive replace
              Ghoulfool Level 1

              Sorry I didn't explain it very well. If the user puts in "layer" as the search criteria it would replace any layers called "layer", "LAYER" or "Layer" The replacement would be to lowercase.

              • 4. Re: Case insensitive replace
                Michael L Hale Level 5

                Yes, "layer", "LAYER" or "Layer" all return 'layer' with toLowerCase() so replace should find and replace any of those three layer names if 'layer' is the search string.

                • 5. Re: Case insensitive replace
                  Ghoulfool Level 1

                  var srcD

                  oc = app.activeDocument;
                  fileName = app.activeDocument.name;

                   

                  var search = new Object();
                  var replace = new Object();
                  // prompt for search criteria
                  search.pattern = prompt('Enter the search pattern to look for for all layers.\n', 'Search');
                  replace.pattern = prompt('Enter the word to REPLACE ' + search.pattern + '.\n', 'Replace');

                   

                  alert("Replacing  '"+ search.pattern + "' to be replaced with '" + replace.pattern + "'.")

                   


                  var mySearch = search.pattern;
                  var myReplace = replace.pattern;

                   

                  var numLayers = srcDoc.layers.length;

                  var layers = srcDoc.layers;


                  processLayers (srcDoc)

                   

                  function processLayers (srcDoc)

                   


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

                   


                           // extra line here
                           var layer = layers[i];
                       
                           if (layer.typename == 'LayerSet')
                           {
                              alert("This is a group")
                           }
                         
                        else
                      
                           {
                       
                        // returning lowercase of string
                        var thisLayer = srcDoc.artLayers[i].name.toLowerCase();
                          // alert (i + " " + layer)

                   

                              tempName = app.activeDocument.artLayers[0].name.toLowerCase();

                   

                           if (thisLayer.search(mySearch) != -1)
                            
                              {
                              tempName = app.activeDocument.artLayers[i].name.toLowerCase();
                              app.activeDocument.artLayers[i].name = tempName.replace(mySearch,myReplace)
                              }
                           }
                        }

                   

                  } // function close

                   

                  The code now works as I want it to. But it ignores groups buts falls over with layers in groups. As i'm not sure how they are indexed I'm a bit stumpted. Is there any documentation on layersets that I might find useful. Cheers

                  • 6. Re: Case insensitive replace
                    Muppet Mark-QAl63s Level 4

                    Ghoulfool, I gave this a go… Ran a few basic tests and I think I got it right? Do let me know… Quite possibly not the most efficient way… But there you go…

                     

                    #target photoshop
                    
                    function layerNamer() {
                    
                         var opts = new Object();
                    
                         var win = new Window('dialog','Rename Layers…');
                    
                         win.preferredSize = [200,undefined];
                    
                         win.opacity = 0.95;
                    
                         var red = 0, green = (1/256)*102, blue = (1/256)*153;
                    
                         win.graphics.backgroundColor = win.graphics.newBrush
                         (win.graphics.BrushType.SOLID_COLOR, [red, green, blue]);
                    
                         var grp1 = win.add('group');
                         grp1.orientation = 'column';
                    
                         var opText = grp1.add('statictext',undefined,'Options: ');
                    
                         opText.graphics.font = ScriptUI.newFont('Helvetica', 'Bold', 12);
                    
                         opText.graphics.foregroundColor = opText.graphics.newPen
                         (opText.graphics.PenType.SOLID_COLOR, [1.0, 1.0, 1.0], 1);
                    
                         grp1.add('statictext', [5,15,195,37], 'Find String/Expression:');
                    
                         var findEdit = grp1.add('edittext', [5,45,195,67], '');
                    
                         findEdit.graphics.backgroundColor = findEdit.graphics.newBrush
                         (findEdit.graphics.BrushType.SOLID_COLOR, [1.0, 1.0, 1.0], 1);
                    
                         findEdit.active = true;
                    
                         grp1.add('statictext', [5,75,195,97], 'Replacement String:');
                    
                         var repEdit = grp1.add('edittext', [5,105,195,127], '');
                    
                         repEdit.graphics.backgroundColor = repEdit.graphics.newBrush
                         (repEdit.graphics.BrushType.SOLID_COLOR, [1.0, 1.0, 1.0], 1);
                    
                         var chkboxA = grp1.add('checkbox', [5,135,195,157], 'Case Sensitive');
                    
                         var chkboxB = grp1.add('checkbox', [5,165,195,187], 'Global');
                    
                         var chkboxC = grp1.add('checkbox', [5,195,195,217], 'Include Text Layers');
                    
                         var grp2 = win.add('group');
                    
                         grp2.cancelBtn = grp2.add('button',undefined,'Cancel',{name:'cancel'});
                    
                         grp2.okBtn = grp2.add('button',undefined,'OK',{name:'ok'});
                    
                         grp2.okBtn.onClick = function() {
                    
                              opts.find = findEdit.text;
                    
                              opts.replace = repEdit.text;
                    
                              opts.ignore = chkboxA.value;
                    
                              opts.global = chkboxB.value;
                    
                              opts.txtLay = chkboxC.value;
                              
                              processDoc(opts);
                    
                              win.close(0);
                    
                         };
                    
                         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,'g'); }
                    
                         if ( !opts.global && !opts.ignore ) { find = new RegExp(opts.find,'i'); }
                         
                         if ( !opts.global && opts.ignore ) { find = new RegExp(opts.find); }
                         
                         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); }
                    
                              }
                    
                         }
                    
                    };
                    

                    • 7. Re: Case insensitive replace
                      Ghoulfool Level 1

                      Mark did tou mean to post this here or maybe on the end of Clevadio's post looking for replacing text (not layer names)

                      http://forums.adobe.com/thread/848745?tstart=0

                      • 8. Re: Case insensitive replace
                        Muppet Mark-QAl63s Level 4

                        I was meant to be here with regard to your last comment about layers within layer sets… if thats what you meant by groups?