Skip to main content
KaylaReily
Participant
September 4, 2012
Question

Find text layer with specific content

  • September 4, 2012
  • 3 replies
  • 1114 views

Ok, i am wondering if there is a way to "scan" an open file in Photoshop for a text layer, then if it finds one to check if it has specific content "Not Clean" (not necessarily case sensitive) but then if it finds it - close the file!? not sure if this can be done or not. thanks for you help!! oh and i am using apple script.

This topic has been closed for replies.

3 replies

Inspiring
September 4, 2012

Quickest may be to have shell GREP the file… It will tell you if there's a match in the binary file…

JJMack
Community Expert
Community Expert
September 4, 2012

Unix shells live on, long live Unix 43 years old and ageing well. GREP on...

JJMack
c.pfaffenbichler
Community Expert
Community Expert
September 4, 2012

Well, this is JavaScript; maybe you can try it anyway.

Changing the line

if (theText.match(new RegExp("not so", "i"))) {

to match your intended text should be no problem.

// close file if certain text is found in type layer;

// 2012, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

var theLayers = checkForText(app.activeDocument, []);

};

////// function //////

function checkForText (theParent) {

          for (var m = theParent.layers.length - 1; m >= 0;m--) {

                    var theLayer = theParent.layers;

                    if (theLayer.typename == "ArtLayer") {

                              if (theLayer.kind == LayerKind.TEXT) {

                                        var theText = theLayer.textItem.contents;

// check for text and close if found;

                                        if (theText.match(new RegExp("not so", "i"))) {

                                                  app.activeDocument.close(SaveOptions.PROMPTTOSAVECHANGES);

                                                  return

                                                  }

                                        }

                              }

// apply the function to layersets;

                    else {

                              allLayers = (checkForText(theLayer))

                              }

                    };

          return allLayers

          };

KaylaReily
Participant
September 4, 2012

Hey thanks! Javascript is cool too... i will give it a try today and let you know! thanks!

Paul Riggott
Inspiring
September 4, 2012

Here is an action manager code example..

main();
function main(){
if(!documents.length) return;
var win = new Window('dialog','Find Text');
win.st1 = win.add('statictext',undefined,'String to Find');
win.et1 = win.add('edittext');
win.et1.preferredSize=[250,20];
win.bu1 = win.add('button',undefined,'Process');
win.bu2 = win.add('button',undefined,'Cancel');
win.bu1.onClick=function(){
if(win.et1.text==''){
    alert("No search string has been entered!");
    return;
    }
win.close(0);
var data = getNamesPlusIDs();
for(var a in data){
var  toSearch = new RegExp (win.et1.text,"i");
    if(data[2].toString().match(toSearch)){
        app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        break;
        }
    }
}
win.center();
win.show();
}
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
        var isLayerSet =( layerType == 'layerSectionContent') ? false:true;
        if(desc.hasKey(stringIDToTypeID('textKey'))){
        descText = desc.getObjectValue(stringIDToTypeID('textKey'));
        var contents = descText.getString( stringIDToTypeID('textKey'));
        Names.push([[Id],[layerName],[contents]]);
        }
   };
return Names;
};

c.pfaffenbichler
Community Expert
Community Expert
September 4, 2012

It can be done with JavaScript at least.

I usually use the DOM as I find it easier to understand, but using Action Manager code might speed things up considerably.