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"))) {
                                                              app.activeDocument.close(SaveOptions.PROMPTTOSAVECHANGES);
                                                              return
                                                              }
                                                    }
                                          }
            // 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..

                     

                     

                    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[a][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;
                    };