Skip to main content
Known Participant
December 23, 2008
Question

CS3 Javascript - finding the paragraph which contain the index page reference

  • December 23, 2008
  • 28 replies
  • 3952 views
Hi,
I've been trying to cook a script which has the following function:
It gets a topic's page reference, and return the index in the story of the paragraph which contains the source text. I figured the line should look something like that (i made it specific for the example):

myParagraphIndex = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.parent.index;

The problem is, that this line return the index of the parent story.

How can I get the index of the containing paragraph??

Thanks,
Ola
This topic has been closed for replies.

28 replies

Inspiring
December 26, 2008
Typo. You got the same paragraph count twice.

Dave
Ola_KAuthor
Known Participant
December 26, 2008
Hi, thanks for the quick reply!

I tryed the revised version and it worked by itslef, so I guess that really was the missing link.

Now, when I combined these 2 lines inside the bigger version, it failed to work. This is the script (Do you have any idea what's the bug?):

//InDesign CS3 Javascript
//Not working properly. Supposed to combine 2 different index markers into a single index marker containing 1 page range
//according to the original index markers location
//The real script has loops and counters, here I simplified it to see what exactely went wrong:

myIndex = app.activeDocument.indexes.item(0);
myTopics = myIndex.topics;
myTopic1 = myTopics.item(0);
myPara1 = myTopics.item(0).pageReferences.item(0).sourceText.paragraphs.item(0);
//The location of the first index marker:
myPara1Place = myPara1.parent.texts.itemByRange(0, myPara1.index).texts.item(0).paragraphs.length;
myTopic2 = myTopics.item(1);
myPara2 = myTopics.item(1).pageReferences.item(0).sourceText.paragraphs.item(0);
//The location of the second index marker:
myPara2Place = myPara1.parent.texts.itemByRange(0, myPara1.index).texts.item(0).paragraphs.length;
//sets the page reference to refer to a page range based on paragraphs:
myTopic1.pageReferences.item(0).pageReferenceType = PageReferenceType.forNextNParagraphs;
myLimit = myPara2Place - myPara1Place;
myTopic1.pageReferences.item(0).pageReferenceLimit = myLimit;
//Removes the second and uneeded topic
myTopic2.pageReferences.item(0).remove();
Inspiring
December 26, 2008
I didn't actually try it and I'm in an Apple Store right now. I think I left out texts[0]. Try this:

myPcount = myPara.parent.texts.itemByRange(0, myPara.index).texts[0].paragraphs.length;

Dave
Ola_KAuthor
Known Participant
December 26, 2008
Hi,

it seems your lines should have solved the issue, but I usually get an error saying "object is invalid" on the second line. It works only if I remove the end:
myPara = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.paragraphs[0]
myPcount = myPara.parent.texts.itemByRange(0, myPara.index)//.paragraphs.length;

Did you try to run these lines and it worked fine?

You know, I think I write the bigger picture:

I am getting a Word file to typeset with index markers inside. The only problem is that Word recognizes PAGE RANGES, while InDesign does not (I mean the ones coming from Word). So my text editor and I agreed on specail "signs" to show where a page range begings and ends.
For example:
INDEX
DogsYYY, 15
DogsZZZ, 20

When I see this I know i need to change the index marker in InDesign to be like that:
INDEX
Dogs, 15-20

Now, when you do it on 1, 2, 3 index markers, it is not so bad. But if you do it on hundreds of them - the fun stops ;) pretty crazy, ha?

So the goal is to make a script to do this simple mechanical action. And this is the bigger picture. I didn't find anything on the internet for it, which is quite surprising... Doesn't anyone else get their text from Word with index markers and page ranges?

Anyway, any sort of help will be greatly appriciated, as I am a *bit* closer to desperate.

Ola
Inspiring
December 26, 2008
I don't get it. The paragraphs are just as likely to change if you're contemplating later editing.

Anyway, the way to get the number of paragraphs is:

myPara = app.activeDocument.indexes.item(0).topics.item(0).pageReferences.item(0).sourceText.paragraphs[0]
myPcount = myPara.parent.texts.itemByRange(0, myPara.index).paragraphs.length;

Dave
Ola_KAuthor
Known Participant
December 26, 2008
You are right. This is also an option. But for my specific script I need it to be according to paragraphs because the page numbers might change, you see, and with paragraphs it is very accurate. I am sure there is some reasonable way to achieve this, but I failed to find it so far... Any clues?



2008/12/26, Robert_Tkaczyk < member@adobeforums.com>:
A new message was posted by Robert_Tkaczyk in


InDesign Scripting --
 CS3 Javascript - finding the paragraph which contain the index page reference

if you need to get page - you don't need paragraph index - you can get page by getting parent of ParentTextFrame of first character of your paragraph


in VB it will be something like this:

PageName = myPara.Characters.Item(1).ParentTextFrames.Item(1).Parent.Name

robin

--
www.adobescripts.com



------------------------------------------------------
View/reply at < http://www.adobeforums.com/webx?13@@.59b75803/2>

Replies by email are OK.
Use the unsubscribe form at < http://www.adobeforums.com/webx?280@@.59b75803!folder=.eea52bc> to cancel your email subscription.


Ola_KAuthor
Known Participant
December 25, 2008
Well, that is a good way to have the paragraph which contains the index marker.

My problem is, that I want to find the index of this paragraph in the story. I mean, is it the 1st, 2nd, 3rd, or 4th (etc) paragraph in the story.

The reason I need to find this index is that I want to use it in the option of setting a topic's page references page range according to a specific number of paragraphs. For example:
INDEX
Cat, 18
Dogs, 20-25 [from a paragraph on page 20, to a paragraph on page 25]

How can I find the number of my paragraph?
Known Participant
December 25, 2008
if you need to get page - you don't need paragraph index - you can get page by getting parent of ParentTextFrame of first character of your paragraph

in VB it will be something like this:

PageName = myPara.Characters.Item(1).ParentTextFrames.Item(1).Parent.Name

robin

--
www.adobescripts.com
Inspiring
December 23, 2008
The parent of the sourceText is the story.

You want sourceText.paragraphs[0]

Dave