Skip to main content
K.Daube
Community Expert
Community Expert
February 17, 2015
Answered

Add paragraphs with text

  • February 17, 2015
  • 1 reply
  • 1768 views

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

// 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;
    oDoc.AddText (textLoc, insText + "\t" + insText); // \t insert a NL...
    oDoc.NewSeriesObject(Constants.FO_Pgf, pgf);  // add a paragraph
    pgf = oDoc.NextPgfInFlow;
  }

This topic has been closed for replies.
Correct answer Klaus Göbel

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.


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;

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

       if (i < nItems -1){

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

        }

      }

1 reply

Klaus Göbel
Legend
February 17, 2015

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.

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 17, 2015

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 ...

K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
February 17, 2015

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.

// 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;
    oDoc.AddText (textLoc, insText + "\x08" + insText); // \t inserts a NL...
    oDoc.NewSeriesPgf(pgf);                       // add a paragraph
  }