jorbas
jorbas

Reputation: 311

Accessing paragraphs added via Javascript

I have a paragraph and after this paragraph, I'd like to add another paragraph. I'm doing this using myPara.contents += "\r". I need this second paragraph to be a different style than the original paragraph. They problem is I cannot seem to get access to this new paragraph to style it.

From what I can tell, this new paragraph is added as a sort of sub paragraph of the first. myPara.paragraphs.count() returns 2. Trying to access it via myPara.paragraphs[1] results in an error. Using myPara.paragraphs[-1] or myPara.paragraphs.lastItem() simply returns the original paragraph.

How can I get an object reference to the new paragraph?

Upvotes: 2

Views: 995

Answers (3)

Abdul H Kidwai
Abdul H Kidwai

Reputation: 1

In my opinion, you will have to refresh your object after you added the new paragraph.

I create a box with text "hello" on a new document.

var mypara = app.activeDocument.pages[0].pageItems[0].paragraphs[0]
mypara.contents //hello
mypara.contents+='\rWorld'

Then I can use, either this

mypara.parent.paragraphs[0].contents //hello
mypara.parent.paragraphs[1].contents //World

or this

var mypara = app.activeDocument.pages[0].pageItems[0].paragraphs
mypara[0].contents
mypaar[1].contents

Thanks, Abdul

Upvotes: 0

Ariel
Ariel

Reputation: 119

Given a reference to a paragraph in InDesign, to add a paragraph to it you should do something like:

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

And then to get a reference to that paragraph you can do this:

myNewPara = myPara.insertionPoints[-1].paragraphs[0];

Upvotes: 1

Loic
Loic

Reputation: 2193

It's likely that when you set — myPara.contents += "\r" — You actually create two paragraphs objects. That you get …count() returns 2 doesn't make much sense to me but what is quite clear here is that myPara.paragraphs will return all the paragraphs objects of the parent paragraph ie myPara.

If you want to index and use the paragraphs here, the parent that has to be targeted is the Story object.

I guess that myPara.parentStory.paragraphs.length will return 2 and myPara.parentStory.paragraphs[0] the first paragraph while myPara.parentStory.paragraphs[-1] the last one of the story.

Upvotes: 1

Related Questions