6 Replies Latest reply on Sep 4, 2012 7:52 AM by Paul Riggott

    Find text layer with specific content

    KaylaReily Level 1

      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.

        • 1. Re: Find text layer with specific content
          c.pfaffenbichler Level 9

          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.

          • 2. Re: Find text layer with specific content
            c.pfaffenbichler Level 9

            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[m];
                                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"))) {
            // apply the function to layersets;
                                else {
                                          allLayers = (checkForText(theLayer))
                      return allLayers
            • 3. Re: Find text layer with specific content
              Muppet Mark Level 5

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

              • 4. Re: Find text layer with specific content
                KaylaReily Level 1

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

                • 5. Re: Find text layer with specific content
                  JJMack Most Valuable Participant

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

                  • 6. Re: Find text layer with specific content
                    Paul Riggott Level 6

                    Here is an action manager code example..



                    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.bu1 = win.add('button',undefined,'Process');
                    win.bu2 = win.add('button',undefined,'Cancel');
                        alert("No search string has been entered!");
                    var data = getNamesPlusIDs();
                    for(var a in data){
                    var  toSearch = new RegExp (win.et1.text,"i");
                    function getNamesPlusIDs(){ 
                       var ref = new ActionReference(); 
                       ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') ); 
                       var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1; 
                       var Names=[];
                    var i = 0; }catch(e){ var i = 1; };
                           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;
                            descText = desc.getObjectValue(stringIDToTypeID('textKey'));
                            var contents = descText.getString( stringIDToTypeID('textKey'));
                    return Names;