2 Replies Latest reply on May 30, 2012 12:13 PM by FrameGurus

    Is there a way to identify when a variable is next to text?

    jaloren28 Level 2

      Lets say I have a variable named foobar, which is in the following paragraph:

       

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tincidunt nisl ut nibh blandit vestibulum. Duis in diam nibh. Vestibulum a ligula vitae diam tincidunt ornare eget ut ante. Quisque at diam et ante elementum foobarultricies. Aenean ut sem nisi. Ut in eros velit, sed consequat diam.

       

      In the above paragraph, the variable foobar is right next to the text ultrices instead of a space between the variable and the text. Is there a way to identify whether a variable is next to text instead of being separated by a space?

        • 1. Re: Is there a way to identify when a variable is next to text?
          4everJang Level 3

          Hello Jaloren,

           

          I have tried the following and it works, assuming you do not have all kinds of markers placed between the variable and the text that follows it. For such cases, you will have to refine the method.

           

          1. I search for the variable using doc.Find. This results in a text range, consisting of two text locations.

          2. From the end location of the variable, I search for the next space character.

          3. If the start location of the space equals the end location of the variable, there is a space.

           

          Note that this may not work if some kind of marker (including element boundaries, index markers, hidden text etc.) are located between the variable and the following text, as the markers all take up space and change the offset of the space that is found. In these cases, you will have to first retrieve the text without the markers into some text string - after which you can use the standard substring functionality to figure out if the space follows the variable.

           

          A similar method can be used to find out about a preceding space, of course.

           

          I hope this helps

           

          Jang

          • 2. Re: Is there a way to identify when a variable is next to text?
            FrameGurus

            Hello Jaloren:

             

            I've written a small extendscript function that fixes and automates the problem you are running into.

             

             

            var doc = app.ActiveDoc
            var variable = doc.FirstVarInDoc
            
            while(variable.ObjectValid())
            {
            var tr1 = variable.TextRange; //text range of the variable
            
                 /*text range for next possible char*/
                var tl1 = new TextLoc(tr1.end.obj, tr1.end.offset + 1);     
                var tl2 = new TextLoc(tr1.end.obj, tr1.end.offset + 2);     
                var tr2  = new TextRange (tl1, tl2)
            
                 var textitems = doc.GetTextForRange (tr2, Constants.FTI_String);
            
                 if (textitems.length > 0)
                 {
                      var str = textitems[0].sdata;
                      if(str != " ") //if space then add text
                      {
                            doc.AddText (tr2.beg, " ");        
                      }        
                 }
                 variable = variable.NextVarInDoc;
            }

             

            Hope this solution nails the problem statement perfectly. Let me know your feedback.

             

            Best Wishes:
            framegurus