1 Reply Latest reply on Nov 11, 2016 1:39 PM by Wosven

    FindGrep() and FindText() together

    tpk1982 Level 4

      Hi All,

       

      Is it possible to use findGrep and findText together?

       

      I need to find the words from TXT file using grep function and then i need to pass that value to findtext.

       

      Screen Shot 2016-11-11 at 3.24.48 PM.png

       

      Tried Coding:

       

      mydoc=app.documents[0];    
      myfndchange();    
      function myfndchange(){    
      var myFolder = File.openDialog("Select the TXT file to Proceed",isSupportedTXT,true);    
      if(myFolder!=null){FilePath=decodeURI(myFolder);}        
      if(myFolder==null){return;}    
      var datafile = new File(FilePath);    
      datafile.open('r') ;    
          
          
      while (!datafile.eof){    
          strLineIn = datafile.readln();    
          colorArray = strLineIn.split("\t");    
          var myColumn1=colorArray[0];    
          var myColumn2=colorArray[1];    
          var flag=0;    
          x=myColumn1.split(" ");    
          app.findGrepPreferences = app.findGrepPreferences = null;     
          app.findGrepPreferences.findWhat = myColumn1 + "(?!/)";    
          var myGrepfound=mydoc.findGrep();    
      //~     alert(myGrepfound.contents)
          ///sending varable to findText
          app.findTextPreferences = app.changeTextPreferences = null;
          app.findChangeTextOptions.caseSensitive = false;
          app.findChangeTextOptions.wholeWord = true;
          
          app.findTextPreferences.findWhat=myGrepfound;
          var myfound=mydoc.findText();
          for(i=0;i<myfound.length;i++){
            if(myfound[i].words.length==x.length){
              app.changeTextPreferences.changeTo  = myColumn1+"/"+myColumn2;
                  myfound[i].changeText();
            }
          
              app.findTextPreferences = null;     app.changeTextPreferences = null;  
          }
      }    
      }
          
          
      function isSupportedTXT(file) {    
      try {    
      if (file instanceof Folder)    
      return true;    
      else    
      return file.name.match(/\.txt$/i) != null;    
      } catch (e) {    
      alert("Error in isSupported method: " + e);    
      }    
      } 
      

       

      Thanks,

      K

        • 1. Re: FindGrep() and FindText() together
          Wosven Level 1

          Hello,

           

          I don't understand why you want to use findText and findGrep to search the same text (myColumn1).

          findText will find more results without "(?!/)" (and replace more).

           

          findGrep result is an array, like findText result.

           

          You can test with this, commenting/uncommenting the 2 parts :

           

          var mydoc=app.documents[0];
          myfndchange();
          
          function myfndchange(){
              // no need for "isSupportedTXT"
              var myTextFile = File.openDialog("Select the TXT file to Proceed", "*.txt",true);
              if(myTextFile != null) { FilePath = decodeURI(myTextFile); }
              else {return false;}
              var datafile = new File(FilePath);
              datafile.open('r') ;
          
          
              app.findGrepPreferences = app.findGrepPreferences = null;
              app.findTextPreferences = app.changeTextPreferences = null;
              app.findChangeTextOptions.caseSensitive = false;
              app.findChangeTextOptions.wholeWord = true;
                  
              while (!datafile.eof) {
                  var strLineIn = datafile.readln();
                  var colorArray = strLineIn.split("\t");
                  
                  if (colorArray.length > 1) {
                      var myColumn1 = colorArray[0];
                      var myColumn2 = colorArray[1];
                      var flag = 0;
                      
                      app.findGrepPreferences.findWhat = myColumn1 + "(?!/)";
                      var myGrepfound = mydoc.findGrep();
                  //*
                      // those 2 lines do the same as below
                      app.changeGrepPreferences.changeTo = myColumn1+"/"+myColumn2;
                      mydoc.changeGrep();
                  //*/
                  /*
                      // sending variable to findText
                      var x = myColumn1.split(" ");
                      for (var f = 0; f < myGrepfound.length; f++) {
                      
                          app.findTextPreferences.findWhat = myGrepfound[f].contents;
                          var myfound = mydoc.findText();
                          for(var i=0; i < myfound.length; i++) {
                              if(myfound[i].words.length == x.length) {
                                  app.changeTextPreferences.changeTo = myColumn1+"/"+myColumn2;
                                  myfound[i].changeText();
                              }
                          }
                      }
                  */
                  }
              }
          }
          

           

          Perhaps there's another pattern. This text is always at the beginning/end of the line ?

          Or you need to change text in a special order, using Grep and "(?!/)".

           

          Swo.