Skip to main content
Participating Frequently
February 8, 2022
Answered

Can't add new paragraph with different paragraphStyle in textFrame

  • February 8, 2022
  • 4 replies
  • 895 views

I tried to add text to end of the text frame with insertionPoints but when I want to change paragraph style of the text which I added recently, all paragraph styles changing in text frame. myTextFrame.paragraphs returned only one paragraph. How can I add a new text with different paragraph style into text frame?

 

 

This topic has been closed for replies.
Correct answer Manan Joshi

You will have to create new paragraph. Just appending the new content will append it to the existing paragraph and hence the style change will be applicable on the prexisiting paragraph as well. Try the following with selecting the textframe, it will add a new pargraph with text Hello and apply pStyle with name PS1 to it

var tf = app.selection[0]
tf.contents += "\rHello"
tf.paragraphs[-1].appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("PS1")

-Manan 

4 replies

Sunil Yadav
Legend
February 12, 2022

Adding an enter mark will do for you just like Manan provided solution.

Community Expert
February 8, 2022

Best work with textFrame.parentStory.insertionPoints[-1] if you want to address the last insertion point because your text frame might run into overset. Also address the last paragraph of textFrame.parentStory.

 

Regards,
Uwe Laubender

( ACP )

Community Expert
February 8, 2022

Just add an end of paragraph character before the position you want to add a new text for a new paragraph.

myTextFrame.parentStory.insertionPoints[-1].contents = "\r";

Regards,
Uwe Laubender

( ACP )

 

Manan JoshiCommunity ExpertCorrect answer
Community Expert
February 8, 2022

You will have to create new paragraph. Just appending the new content will append it to the existing paragraph and hence the style change will be applicable on the prexisiting paragraph as well. Try the following with selecting the textframe, it will add a new pargraph with text Hello and apply pStyle with name PS1 to it

var tf = app.selection[0]
tf.contents += "\rHello"
tf.paragraphs[-1].appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("PS1")

-Manan 

-Manan
mangokafaAuthor
Participating Frequently
February 8, 2022

Thank you for your answer but all styles mixed up. I added first text and it's style. There was no problem. Then I added second content and it's style. Now first part has second style and second part has first style. How should I change the paragraphs index for each content addition?

Community Expert
February 8, 2022

Share the exact code snippet you are using and if possible a demo document.

-Manan

-Manan