4 Replies Latest reply on Nov 19, 2015 5:21 AM by frameexpert

    How to define the offset for a textrange (after a TAB)?

    K.Daube Level 1

      Dear experts,

      I want to grab the part after TAB in a paragraph, for example from
      Something TAB rest of the paragraph using some character formatting.
      I have no clue how to find the value for the offset.

      Converting the pgf to text and then look for the TAB will obviously provide a wrong value - or not?

      Will it be necessary to iterate through the paragraph (moving the text range from char to char) to find the item behind the TAB?

      Can You please enlighten me ?

      var doc = app.ActiveDoc;
      var mainflow = doc.MainFlowInDoc;
      var tframe = mainflow.FirstTextFrameInFlow;
      var pgf = tframe.FirstPgf;
      
      GetPgfPartAfterTAB (pgf);
      
      function GetPgfPartAfterTAB (oPgf) {
        var oDoc = app.ActiveDoc;
        var tRange= new TextRange();
        tRange.beg.obj = oPgf;
        tRange.beg.offset = 0;           // Well, this must be calculated!
        tRange.end.obj = oPgf;
        tRange.end.offset = Constants.FV_OBJ_END_OFFSET;
        oDoc.TextSelection = tRange;
      
        oDoc.Copy (0);                  // for test
      }
      
        • 1. Re: How to define the offset for a textrange (after a TAB)?
          frameexpert Level 4

          Hi Klaus, Here is how you do it with the paragraph containing your insertion point.

           

          #target framemaker
          
          var doc = app.ActiveDoc;
          var pgf = doc.TextSelection.beg.obj;
          
          // Make a text location at the beginning of the paragraph.
          var textLoc = new TextLoc (pgf, 0);
          // Get the find parameters for finding a tab.
          var findParams = getFindParams ("\x08");
          // Search for the string starting at the beginning of the paragraph.
          var textRange = doc.Find (textLoc, findParams);
          // See if the tab was found.
          if (textRange.beg.obj.ObjectValid ()) {
              // Make sure the tab is in the correct paragraph.
              if (textRange.beg.obj.Unique === pgf.Unique) {
                  // Select the text from the beginning of the tab
                  // up to the end of the paragraph.
                  var tabRange = new TextRange (new TextLoc (pgf, textRange.beg.offset),
                      new TextLoc (pgf, Constants.FV_OBJ_END_OFFSET - 1));
                  // Select the range for illustrative purposes.
                  doc.TextSelection = tabRange;
              }
          }
          
          function getFindParams (string) {
            
              var findParams = AllocatePropVals (1);
            
              findParams[0].propIdent.num = Constants.FS_FindText;
              findParams[0].propVal.valType = Constants.FT_String;
              findParams[0].propVal.sval = string;
                
              return findParams;
          }
          
          
          

           

          Once you get to line 20, you are ready to apply the character format. Note that line 21 is not necessary in order to apply the character format, but to illustrate that you have the desired range selected. You can remove this from your final code.

           

          Also, I would prefer use one or more functions to encapsulate this, but I wanted to go from top to bottom so you can understand what is actually happening for each paragraph. I will try to post something more later. Thanks. -Rick

           

          PS. FrameScript makes this much easier:

           

          Set oDoc = ActiveDoc;
          Set oPgf = oDoc.TextSelection.Begin.Object;
          Find String(CHARTAB) InObject(oPgf) ReturnStatus(iStatus) ReturnRange(tRange);
          If iStatus = 1 // tab found.
            New TextRange NewVar(tTabRange) Object(oPgf) Offset(tRange.Begin.Offset)
              Offset(ObjEndOffset-1);
            TextSelection = tTabRange;
          EndIf
          
          
          • 2. Re: How to define the offset for a textrange (after a TAB)?
            frameexpert Level 4

            Here is a version that encapsulates the code for finding the text range into a function. There is also a function for applying a character format. In my code, I am using "Emphasis".

             

            #target framemaker
            
            var doc = app.ActiveDoc;
            var pgf = doc.TextSelection.beg.obj;
            // Get the text range starting with the tab to the end of the paragraph.
            var tabRange = getTabRange (pgf, doc);
            if (tabRange) {
                // Apply the character format.
                applyCharFmt (tabRange, "Emphasis", doc);
            }
            
            function getTabRange (pgf, doc) {
                
                var textLoc, textRange, findParams, tabRange;
            
                // Make a text location at the beginning of the paragraph.
                textLoc = new TextLoc (pgf, 0);
                // Get the find parameters for finding a tab.
                findParams = getFindParams ("\x08");
                // Search for the string.
                textRange = doc.Find (textLoc, findParams);
                // See if the tab was found.
                if (textRange.beg.obj.ObjectValid ()) {
                    // Make sure the tab is in the correct paragraph.
                    if (textRange.beg.obj.Unique === pgf.Unique) {
                        // Select the text from the beginning of the tab
                        // up to the end of the paragraph.
                        tabRange = new TextRange (new TextLoc (pgf, textRange.beg.offset),
                            new TextLoc (pgf, Constants.FV_OBJ_END_OFFSET - 1));
                        // Return the text range.
                        return tabRange;
                    }
                }
                // Not found; return 0
                return 0;
            }
            
            function applyCharFmt (textRange, name, doc) {
                
                var charFmt = 0;
                
                charFmt = doc.GetNamedCharFmt (name);
                if (charFmt.ObjectValid()) {
                    doc.SetTextProps (textRange, charFmt.GetProps());
                }
            }
            
            function getFindParams (string) {
                
                var findParams = AllocatePropVals (1);
                
                findParams[0].propIdent.num = Constants.FS_FindText;
                findParams[0].propVal.valType = Constants.FT_String;
                findParams[0].propVal.sval = string;
                    
                return findParams;
            }
            
            • 3. Re: How to define the offset for a textrange (after a TAB)?
              K.Daube Level 1

              Thank You, Ric - works fine!

              I was not that clear with my sample "Something TAB rest of the paragraph using some character formatting".

              I just meant that the paragraph may contain character formats - in contrast to just a string of text.

              Looking for just the text after the TAB i added a "+1" in line 28:

              tabRange = new TextRange (new TextLoc (pgf, textRange.beg.offset+1), new TextLoc (pgf, Constants.FV_OBJ_END_OFFSET - 1));
              
              
              

              But I noted one strange thing:

              When at the start of the script the cursor is not at the beginning of a paragraph, then the test for Uniqueness fails.

              - For pgf.Unique I get the id of the current pgf,

              - for textRange.beg.obj.Unique i get the id of the next paragraph.

              alert (textRange.beg.obj.Unique + "    " + pgf.Unique);   //?? 
              if (textRange.beg.obj.Unique === pgf.Unique) {  // Make sure the tab is in the correct paragraph.
              
              
              

              But in the context of my project this is not an issue, because one pgf after the other is worked off.

               

              Again, thank You very much.

              • 4. Re: How to define the offset for a textrange (after a TAB)?
                frameexpert Level 4

                Hi Klaus, I am not sure exactly what is happening as far as the uniqueness issue, but if it becomes a problem, let me know. If you are satisfied with my answer, please mark it as correct. Thank you. -Rick