6 Replies Latest reply on Feb 10, 2014 5:10 PM by gisteppen

    TextLoc works once, then stops working

    gisteppen Level 1

      I have this simple code:

       

      var doc = app.ActiveDoc;

      var t1 = new TextLoc();

      var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

      t1.obj = firstPgf;

      doc.AddText (t1, "Hello");

       

      When I ran it the first time from the ES toolkit code window, it printed "Hello" on the first line of the active Fm document. But when I ran the code again a moment later on the same doc, nothing happens. There are no visible errors and it prints "Result: [object TextLoc]" in the JavaScript console.

       

      I can't get it to run again. This happened a few days ago and I gave up. Then just now I decided to try it again and it did the same thing - printed "Hello" once in the Fm doc, then it would not do it again. I've tried restarting Fm and the toolkit.

       

      Any ideas what is causing this?

       

      Thanks,

      Mark

        • 1. Re: TextLoc works once, then stops working
          frameexpert Level 4

          Hi Mark, Apparently there is a problem with the order of the commands. If you try it this way, it appears to work:

           

          var doc = app.ActiveDoc;
          
          var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;
          var t1 = new TextLoc(firstPgf,0);
          
          doc.AddText (t1, "Hello");
          

           

          Rick

          • 2. Re: TextLoc works once, then stops working
            gisteppen Level 1

            Yay! That works. Thanks Rick.

            • 3. Re: TextLoc works once, then stops working
              4everJang Level 3

              Hi Mark (and Rick),

               

              I want to correct something in your understanding of the TextLoc object.

               

              The order of commands does not have anything to do with the problem that the code only ran once after restart and then stopped. The issue is that the TextLoc contains a pointer to the object and an offset, and both must be initialized. If you add one line to the code, as shown below, the script works fine every time.

               

              var doc = app.ActiveDoc;

              var t1 = new TextLoc();

              var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

              t1.obj = firstPgf;

              t1.offset = 0;

              doc.AddText (t1, "Hello");

               

              If you omit setting the offset, it will accidentally have the value 0 when it is first used, but it gets changed to something undefined when adding text - there is no way to tell what FM makes of it. If the value is outside of the range of the object you are pointing to, FM will simply refuse to add the text.

               

              The reason why Rick's code worked is that he used another method of creating the TextLoc object, which takes the object and an offset as reference explicitly. That of course works anytime.

               

              Kind regards

               

              Jang

              • 4. Re: TextLoc works once, then stops working
                frameexpert Level 4

                Great points Jang. Thanks for clarifying. --Rick

                • 5. Re: TextLoc works once, then stops working
                  gisteppen Level 1

                  Ah! That's nice. Thanks Jang, you are making me smarter.  Mark

                  • 6. Re: TextLoc works once, then stops working
                    gisteppen Level 1

                    By the way, I realized where my original flawed code came from: The FrameMaker 10 Scripting Guide, p.6. It gives this sample:

                     

                    var doc = app.ActiveDoc;

                    var tl = new TextLoc();

                    var firstPgf = doc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;

                    tl.obj = firstPgf;

                    doc.AddText (tl, "Hello");

                    doc.AutoChangeBars =1;

                     

                    FrameMaker ExtendScript tech writers please take note that this sample needs t1.offset = 0; added or it only works one time.