Skip to main content
Participant
December 13, 2012
Question

How to create paragraphs from scratch and insert them into a text frame.

  • December 13, 2012
  • 1 reply
  • 1852 views

How do I create paragraph objects within a script, set their contents, set the paragraph style and then insert them into a text frame?

For example how would I create the following entirely within a script?

I'm using InDesign CS6 and coding in Javascript.

This topic has been closed for replies.

1 reply

csm_phil
Legend
December 13, 2012

Hi davehaigh,

Try the below JS code.

This code will create the new document and create the new text frame and insert the text like "Some text in this paragraph" and create the paragraph styles "TEST" apply this style to the entire contents.

var myDoc = app.documents.add();

myDoc.paragraphStyles.add({name:"TEST"})

var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});

myTF.contents = 'Some text in this paragraph\nSome text in this paragraph\nSome text in this paragraph';

myTF.parentStory.appliedParagraphStyle = myDoc.paragraphStyles.item("TEST");

thx,

csm_phil

davehaighAuthor
Participant
December 13, 2012

Thanks Phil,

That creates one paragraph with one style name.

Do you know how would I generate what I have in the screenshot? i.e. multiple paragraphs with varying style names?

Jongware
Community Expert
Community Expert
December 13, 2012

I use one of two methods, whichever is more convenient (or less inconvenient).

The first way needs to know in advance how many paragraphs are to be added (and in the case of your purple text, which nth word is going to be colored). It has the advantage that inserting text in bulk  is very fast.

The second way is to only append text at the end of your current text, setting the 'insertionpoint' properties to whatever needs changing. Slow, but if you do not know up front what text gets what formatting, it's the only way.

(Well, that or you need to do a lot of book-keeping, saving the number of added paragraphs and whatnot.)

alert ("method 1");
var myDoc = app.documents.add();
myDoc.paragraphStyles.add({name:"Test Name 1"})
myDoc.paragraphStyles.add({name:"Test Name 2"})
myDoc.colors.add({space:ColorSpace.RGB, colorValue:[255, 0, 255], name:"purply"});
var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});

myTF.contents = "Some text in this paragraph\rSome text in this paragraph";
myTF.paragraphs[0].appliedParagraphStyle = "Test Name 1";
myTF.paragraphs[1].appliedParagraphStyle = "Test Name 2";
myTF.paragraphs[1].words[4].fillColor = "purply";


alert ("method 2");

var myDoc = app.documents.add();
myDoc.paragraphStyles.add({name:"Test Name 1"})
myDoc.paragraphStyles.add({name:"Test Name 2"})
myDoc.colors.add({space:ColorSpace.RGB, colorValue:[255, 0, 255], name:"purply"});
var myTF = myDoc.pages[0].textFrames.add({geometricBounds:['3p','3p', '30p', '30p']});

myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 1";
myTF.insertionPoints[-1].contents = "Some text in this paragraph\r";
myTF.insertionPoints[-1].appliedParagraphStyle = "Test Name 2";
myTF.insertionPoints[-1].contents = "Some text in this ";
myTF.insertionPoints[-1].fillColor = "purply";
myTF.insertionPoints[-1].contents = "paragraph";