5 Replies Latest reply on Dec 2, 2016 1:54 AM by birdinatree

    Script to search table in selected text frame

    birdinatree

      Hello,

       

      I'm trying to get a script for applying some cell styles based on content to work. Since I'm really new to scripting I can't get this to work as I want to.

       

      The following script does what I want, but it applies to every table in the document. I just want it to apply it on the tables in a selected text frame.

       

      Can anyone help me with this?

       

      Also, is it possible to have more than one word to search for?

       

      var curDoc = app.activeDocument;  
      var allTables = curDoc.stories.everyItem().tables.everyItem();   
        
      app.findTextPreferences = app.changeTextPreferences = null;  
      app.findTextPreferences.findWhat = "Vev";  
      var allFounds = allTables.findText();  
      app.findTextPreferences = app.changeTextPreferences = null;  
        
      for ( var i = 0; i < allFounds.length; i++ ) {  
          var tableFound = allFounds[i];  
          if ( tableFound.length > 0 ) {  
              for ( var j = 0; j < tableFound.length; j++ ) {  
                  var curFound = tableFound[j];  
                  var cellsInRow = curFound.parent.parentRow.cells.everyItem();           
                  cellsInRow.appliedCellStyle = curDoc.cellStyles.itemByName( "Reglage line" );  
                  cellsInRow.clearCellStyleOverrides( true );  
              }
          }
      }
      
        • 1. Re: Script to search table in selected text frame
          [Jongware] Most Valuable Participant

          1. Don't use curDoc.stories.everyItem().tables.everyItem();, use app.selection[0].tables.everyItem();

          (where 'app.selection[0]' is your current selection - so make sure a text frame is selected).

           

          2. Just add it to the find text:

           

          app.findTextPreferences.findWhat = "Word1 word2";

          1 person found this helpful
          • 2. Re: Script to search table in selected text frame
            birdinatree Level 1

            Hello Jongware,

             

            Thank you very much! app.selection[0].tables.everyItem() worked perfectly

            In my own attempts I missed [0] after app.selection.

             

            I realized that I was a bit unclear on the second question.

             

            What I want to achieve is to have different search phrases. So I guess I have to create some kind of loop to first search for "phrase 1" and then apply the cell style, then "phrase 2" and apply cell style for that row and so on.

             

            Thanks again and I will mark the question as answered since it solved my main issue.

            • 3. Re: Script to search table in selected text frame
              [Jongware] Most Valuable Participant

              Ah, no problem either.

               

              You could issue two separate findText commands, but then you'd have to run the loop twice as well. In this case, you are better off with a GREP search instead, which can look for more than one phrase.

               

              The main difference between GREP and the regular find is that GREP is case sensitive by default, whereas findText is case insensitive. So to do the same (if necessary), add '(?i)' at the front of the expression. Also, in findText you can instruct it to find entire words only (although you don't set that in the findTextPreferences in your own word), and in GREP you have to add a few more codes.

               

              Try this:

              app.findGrepPreferences = null;
              app.findGrepPreferences.findWhat = "(?i)\\b(word1|word2)\\b";
              var allFounds = allTables.findGrep();
              

               

              You don't need to set changeGrepPreferences because you use findGrep and not changeGrep.

              The \b codes are the word delimiters; they need their backslashes doubled because this is a JavaScript string, and if you don't, the backslashed code would be parsed before it gets handed over to GREP. (A common pitfall; there must be hundreds of questions about this here! So best to get used to it right away.)

              The parenthesized "word1|word2" is a regular GREP "x or y".

              1 person found this helpful
              • 4. Re: Script to search table in selected text frame
                birdinatree Level 1

                Thank you very much! This is exactly what I was looking for.

                 

                I'll post my final code here for future reference where I added an if statement to ensure a text frame is selected.

                 

                if (app.selection.length > 0 && app.selection[0] instanceof TextFrame) {
                
                  var curDoc = app.activeDocument;  
                  var allTables = app.selection[0].tables.everyItem(); 
                
                  app.findGrepPreferences = null;  
                  app.findGrepPreferences.findWhat = "(?i)\\b(word1|word2|word3)\\b";  
                  var allFounds = allTables.findGrep();
                   
                  for ( var i = 0; i < allFounds.length; i++ ) {  
                     var tableFound = allFounds[i];  
                     if ( tableFound.length > 0 ) {  
                         for ( var j = 0; j < tableFound.length; j++ ) {  
                             var curFound = tableFound[j];  
                             var cellsInRow = curFound.parent.parentRow.cells[0];           
                             cellsInRow.appliedCellStyle = curDoc.cellStyles.itemByName( "YourCellStyle" );  
                             cellsInRow.clearCellStyleOverrides( true );  
                         }
                     }
                  }
                } else {
                  alert("Please select a text frame!")
                }
                
                • 5. Re: Script to search table in selected text frame
                  Peter Kahrel Adobe Community Professional & MVP

                  > The parenthesized "word1|word2" is a regular GREP "x or y".

                   

                  It's acually "x and y" as it finds both

                   

                  P.