Copy link to clipboard
Copied
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?
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
Share the exact code snippet you are using and if possible a demo document.
-Manan
Copy link to clipboard
Copied
myLeftTextFrame.contents = "1.\t";
myLeftTextFrame.contents += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras hendrerit lacinia dapibus. Curabitur sodales sapien risus, eu rhoncus mi rutrum ac. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent placerat erat vel augue sollicitudin bibendum. In lacus dolor, imperdiet vel aliquet non, feugiat quis enim. Aenean vel ligula ligula. Integer magna lectus, commodo in fermentum condimentum, feugiat sit amet leo.\n\n";
myLeftTextFrame.recompose();
myLeftTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
myLeftTextFrame.parentStory.paragraphs.everyItem().justification = Justification.LEFT_JUSTIFIED;
var rect = myLeftTextFrame.insertionPoints.item(-1).rectangles.add( {geometricBounds:[0,0,40,92.5], strokeWeight:0} );
//myLeftTextFrame.paragraphs[-1].appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("ST1");
rect.place ("/Users/pc/Documents/SFTP/metafly.ai");
rect.fit( FitOptions.CONTENT_TO_FRAME );
rect.fit( FitOptions.PROPORTIONALLY);
myLeftTextFrame.fit(FitOptions.FRAME_TO_CONTENT);
myLeftTextFrame.insertionPoints.item(-1).contents = "\rLorem ipsum dolor sit amet, consectetur adipiscing elit.\n";
//myLeftTextFrame.paragraphs.lastItem().appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("ST2");
myLeftTextFrame.insertionPoints.item(-1).contents = "\rCras hendrerit lacinia dapibus. Curabitur sodales sapien risus, eu rhoncus mi rutrum ac.\n";
//myLeftTextFrame.paragraphs.lastItem().appliedParagraphStyle = app.documents[0].paragraphStyles.itemByName("ST3");
Commented lines are the styles that I am trying to add. You can see the output at attachment. (And I think there is a problem with the sequence of the texts. Second one appeares at the bottom.)
Copy link to clipboard
Copied
The problem I see is that you are calling the fitting the textframe to content after adding in the first paragraph. This will cause an overset with the addition of new text and elements and hence the last paragraph will return only the paragraph that is visible and that is the first one.
Try this make the textframe large enough to contain all you text and elements and then remove the fit method call from your code, it should work
-Manan
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
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 )
Copy link to clipboard
Copied
Adding an enter mark will do for you just like Manan provided solution.