Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Case insensitive replace

Engaged ,
Apr 28, 2011 Apr 28, 2011

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.name;

if (thisLayer.search(mySearch) != -1)
{
   tempName = app.activeDocument.artLayers.name
   app.activeDocument.artLayers.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

TOPICS
Actions and scripting
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Guide ,
Apr 28, 2011 Apr 28, 2011

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…

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 28, 2011 Apr 28, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 28, 2011 Apr 28, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Apr 28, 2011 Apr 28, 2011

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 04, 2011 May 04, 2011

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;
     
         if (layer.typename == 'LayerSet')
         {
            alert("This is a group")
         }
       
      else
    
         {
     
      // returning lowercase of string
      var thisLayer = srcDoc.artLayers.name.toLowerCase();
        // alert (i + " " + layer)

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

         if (thisLayer.search(mySearch) != -1)
          
            {
            tempName = app.activeDocument.artLayers.name.toLowerCase();
            app.activeDocument.artLayers.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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 09, 2011 May 09, 2011

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.name) )                     {                          if (layObj.kind == LayerKind.TEXT && !opts.txtLay) { continue; }                                                    layObj.name = layObj.name.replace(find,opts.replace);                     }                if ( layObj.typename == 'LayerSet' )                     { recurseLayers(layObj.layers); }           }      } };

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
May 10, 2011 May 10, 2011

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 10, 2011 May 10, 2011
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines