5 Replies Latest reply on Jul 25, 2015 7:43 AM by frameexpert

    Running script on conditions

    che47audio Level 1

      I have a document with approximately 50 tables in it, some of which I need to add some text and shade some cells etc. The tables which I need to run my script over are identified by a string ( just one word ) found in the second row, first cell of these particular tables. I need to search each table in the document for this string, if the string is present then apply the script, if not present then move to the next table in the document. I've had good read around "getText" but I'm unsure if this the right way to go and still don't know how to apply it. Anybody any ideas how I could achieve this?

       

      Thanks

       

      Chris

        • 1. Re: Running script on conditions
          frameexpert Level 4

          Here is user-defined function that will get text from any FrameMaker text object (Pgf, Cell, TextFrame, etc.) or TextRange. Please let me know if you have any questions or comments.

           

          function getText (textObj, doc) {
              // Gets the text from the text object.
          
              var text = "";
              // Get a list of the strings in the text object or text range.
              if (textObj.constructor.name !== "TextRange") {
                  var textItems = textObj.GetText(Constants.FTI_String);
              } else {
                   var textItems = doc.GetTextForRange(textObj, Constants.FTI_String);
              }
              // Concatenate the strings.
              for (var i = 0; i < textItems.len; i += 1) {
                  text += (textItems[i].sdata);
              }
              return text; // Return the text
          }
          
          • 2. Re: Running script on conditions
            che47audio Level 1

            Hi Rick, thanks for the help once again.

             

            So the "TextRange" is the string/word that I want to find?  I don't understand the two textItems that you have put for the if else variables.


            And the concatenating of the strings, please could you explain this part of what you've written?


            Thanks


            Chris

            • 3. Re: Running script on conditions
              frameexpert Level 4

              Hi Chris,

               

              No, TextRange has nothing to do with the text you are trying to find. The function doesn't find any particular text, it just returns all of the text of a given text object (Pgf, Cell, TextLine, etc.) or TextRange (for example, a text selection). There are two separate methods for getting text items; one is for text objects (line 07) and the other for text ranges (line 09). In both cases, the methods are getting a list of strings (FTI_String) in the text object or text range. Since there could be more than on string in the object or range, lines 12-14 loop through them and concatenate them into a single string. This string gets returned on line 15.

               

              Rick

              • 4. Re: Running script on conditions
                che47audio Level 1

                Hi Rick,

                 

                Thanks, that's a little clearer now. In this case I would require to return text from a text object, a table cell.

                How do I specify a specific cell of the currently selected table?  And when it returns the text I can then use "text" as a condition, e.g if the string "text" is equal to "XXXXX" then run the following script?

                 

                Thanks

                 

                Chris

                • 5. Re: Running script on conditions
                  frameexpert Level 4

                  Hi Chris, There are basically two ways to navigate through table cells. You can do it row-by-row:

                   

                  var doc = app.ActiveDoc;
                  var tbl = doc.SelectedTbl;
                  var row = tbl.FirstRowInTbl;
                  var cell;
                  
                  while (row.ObjectValid () === 1) {
                      cell = row.FirstCellInRow;
                      while (cell.ObjectValid () === 1)  {
                          // Do something here.
                          // ...
                          cell = cell.NextCellInRow;
                      }
                      row = row.NextRowInTbl;
                  }
                  

                   

                  Or you can go cell-by-cell:

                   

                  var doc = app.ActiveDoc;
                  var tbl = doc.SelectedTbl;
                  var row = tbl.FirstRowInTbl;
                  var cell = row.FirstCellInRow;
                  
                  while (cell.ObjectValid () === 1) {
                      // Do something here.
                      // ...
                      cell = cell.NextCellInTbl;
                  }
                  

                   

                  -Rick