Search code examples
adobe-indesignextendscript

Create a textFrame with multiple paragraphs


From what I've been reading and experimenting by myself when you add a new textFrame it adds automatically a paragraph inside of it so you can do:

const tf = myPage.textFrames.add();
const myParagraph = tf.paragraphs.item(0);
// and then format myParagraph

But what if I need to add multiple paragraphs?

I know I can do something like this:

const tf = myPage.textFrames.add();
tf.contents = 'AAA\nBBB\nCCC'

But is there a better way (a more javascript like way) to add more paragraphs to a textFrame? something like (pseudo code):

const tf = myPage.textFrames.add();
const myParagraphA = tf.paragraphs.add();
myParagraphA.contents = 'Content of paragraph A'
const myParagraphB = tf.paragraphs.add();
myParagraphB.contents = 'Content of paragraph B'
const myParagraphC = tf.paragraphs.add();
myParagraphC.contents = 'Content of paragraph C'

Solution

  • I like to use the join() method:

    var p1 = 'Content of paragraph A';
    var p2 = 'Content of paragraph B';
    var p3 = 'Content of paragraph C';
    var tf = myPage.textFrames.add();
    tf.contents = [p1,p2,p3].join('\n');