7 Replies Latest reply on Feb 18, 2015 9:23 AM by K.Daube

    Add paragraphs with text

    K.Daube Level 1

      Dear patient helpers,

      I want to 'dump' the contents of an array into a series of paragraphs. Two problems appear in my script:

      - inserting a TAB by \t inserts a NewLine

      - items are not placed in succesive paragraphs but at the start of the flow - hence in reverse order and not in own paragraphs

      t-write-array.png

      // write array to multiple paragraphs
        var items      = new Array ();
        var nItems, pgf, textLoc, oDoc; 
        items = ["Århus", "Çedille", "Dandy"];
        nItems = items.length;
        oDoc = app.ActiveDoc;
        
        pgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;  // get first pgf in flow
        textLoc = new TextLoc (pgf, 0);  
        for (var i = 0; i < nItems; i += 1) {
          insText = items[i];
          oDoc.AddText (textLoc, insText + "\t" + insText); // \t insert a NL...
          oDoc.NewSeriesObject(Constants.FO_Pgf, pgf);  // add a paragraph
          pgf = oDoc.NextPgfInFlow;
        }
      
      
      
        • 1. Re: Add paragraphs with text
          Klaus Göbel Level 3

          Hi Klaus,

           

          first of all you should swap lines 9 and 10, because you ALWAYS use the same textLoc. And so all textitems are inserted at the beginning.

          • 2. Re: Add paragraphs with text
            K.Daube Level 1

            Thanks Klaus, for the idea.

            However the result of this swap is that for the second iteration (i=1) I get the error on this line :

            Bad argument {LiveObject("TextLoc").property(0)}.

            Obviously i do not understand the concepts of text location and paragraphs ...

            • 3. Re: Add paragraphs with text
              K.Daube Level 1

              At least one step further:

              To get a TAB inserted, the string must be "\x08" and not "\t" or "\x09".

              And with the following code i get additional paragraphs, but the textloc does not advance into these.

              t-write-array2.png

              // write array to multiple paragraphs
                var items      = new Array ();
                var nItems, pgf, textLoc, oDoc; 
                items = ["Århus", "Çedille", "Dandy"];
                nItems = items.length;
                oDoc = app.ActiveDoc;
                
                pgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;  // get first pgf in flow
                for (var i = 0; i < nItems; i += 1) {
                  textLoc = new TextLoc (pgf, 0);  
                  insText = items[i];
                  oDoc.AddText (textLoc, insText + "\x08" + insText); // \t inserts a NL...
                  oDoc.NewSeriesPgf(pgf);                       // add a paragraph
                }
              
              • 4. Re: Add paragraphs with text
                Klaus Göbel Level 3

                your last line is missing now : pgf = oDoc.NextPgfInFlow; 

                1. for (var i = 0; i < nItems; i += 1) { 
                2.     textLoc = new TextLoc (pgf, 0);   
                3.     insText = items[i]; 
                4.     oDoc.AddText (textLoc, insText + "\x08" + insText); // \t inserts a NL... 
                5.     oDoc.NewSeriesPgf(pgf);                       // add a paragraph 
                6.     pgf = oDoc.NextPgfInFlow;
                7.   } 
                • 5. Re: Add paragraphs with text
                  K.Daube Level 1

                  Klaus, it seems that the situation is more complicated.

                  Adding the missing line creates the error

                     Bad argument {LiveObject("TextLoc").property(0)}

                  for line 9 (your line 2).

                  Not understanding the required functions/methods my approach resembles "im nebel stochern" -  poking in the fog. Unfortunately there are no examples for this case available (and I'm still in the early chinese phase aka copying existing solutions) - most use of eScript handles existing content - but I want to create some content.

                  • 6. Re: Add paragraphs with text
                    Klaus Göbel Level 3

                    Hi Klaus,

                     

                    just change your last line from

                     

                    oDoc.NewSeriesPgf(pgf);  

                     

                    to :

                    pgf = oDoc.NewSeriesPgf(pgf);  

                     

                    that should do it.

                    So you directly point to the new pgf.

                     

                    completely :

                    // write array to multiple paragraphs

                          var items      = new Array ();

                          var nItems, pgf, textLoc, oDoc; 

                          items = ["Århus", "Çedille", "Dandy"];

                          nItems = items.length;

                          oDoc = app.ActiveDoc;

                          

                          pgf = oDoc.MainFlowInDoc.FirstTextFrameInFlow.FirstPgf;  // get first pgf in flow

                          for (var i = 0; i < nItems; i += 1) {

                            textLoc = new TextLoc (pgf, 0);  

                            insText = items[i];

                            oDoc.AddText (textLoc, insText + "\x08" + insText); // \t inserts a NL...

                     

                           if (i < nItems -1){

                            pgf = oDoc.NewSeriesPgf(pgf);                       // add a paragraph 

                            }

                          }

                    • 7. Re: Add paragraphs with text
                      K.Daube Level 1

                      Klaus, congratulation!

                      This is the solution. The script now works as intenden!

                      How many beers do I owe You? (a well, today is Ash Wednesday, so for me there is no beer in reach).