Skip to main content
March 9, 2011
Answered

[JS] number of all PageReferences of all Topics

  • March 9, 2011
  • 1 reply
  • 522 views

Hi,

I'm trying to make a progress bar while the treatment of my topics and (obviously) I need to know how much pageReferences of my topics I have in my document.

With this I know how many topics there is :

myDocument.indexes.item(0).allTopics.length

but for each topic I can have many pageReferences. How can I know how much there is?

An other question :

Is there a possibility to start the treatment of the topics at the beginning of the document?

for example :

chicken at page 2

egg at page 1 and 3

(Just an example...)

I would like to treat egg at page 1, then chicken at page 2, and egg at page 3.

For now I do chicken then egg (1) then egg (2).

Hope someone will help me!

This topic has been closed for replies.
Correct answer Peter Kahrel

Aha -- you want to find topics/page references for a story? That's a bit more complicatedbecause topics are children of the index, and the index is a child of the document. So there's no direct link between topics/page references and stories. So what you would need to do is to get all page references, get each one's parent story (using sourceText), compare it with your story, and if the two stories are the same, put the page reference's parent topic somewhere, e.g. in an array.

Given a reference story MyStory, this script

 
// Collect all page references
PRefs = app.documents[0].indexes[0].topics.everyItem().pageReferences.everyItem().getElements();
StoryTopics = [];
for (i = 0; i < PRefs.length; i++)
    // PRefs.sourceText is an InsertionPoint
    if (PRefs.sourceText.parentStory == MyStory)
        PRefs.parent.push (StoryTopics);

places all topics that occur in myStory in the array StoryTopics. You could represent your topics/stories in several different ways, your choice depends on what you want to do with your data.

Peter

1 reply

Peter Kahrel
Community Expert
Community Expert
March 11, 2011

You can probably get the number of page references as follows:

myTopic.pageReferences.length;

>I would like to treat egg at page 1, then chicken at page 2, and egg at page 3.

That's more like a table of contents than an index.

Peter

March 11, 2011

Thant you for answering.

Yes, I know I can do that, but that's not what I am looking for.

I need something who would take myStory and returns all the topics who were found (and who have a page reference).

No, no, it's an index!

(My english is maybe bad, I'm sorry, I'm french and I'm not very good in english...)

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
March 13, 2011

Aha -- you want to find topics/page references for a story? That's a bit more complicatedbecause topics are children of the index, and the index is a child of the document. So there's no direct link between topics/page references and stories. So what you would need to do is to get all page references, get each one's parent story (using sourceText), compare it with your story, and if the two stories are the same, put the page reference's parent topic somewhere, e.g. in an array.

Given a reference story MyStory, this script

 
// Collect all page references
PRefs = app.documents[0].indexes[0].topics.everyItem().pageReferences.everyItem().getElements();
StoryTopics = [];
for (i = 0; i < PRefs.length; i++)
    // PRefs.sourceText is an InsertionPoint
    if (PRefs.sourceText.parentStory == MyStory)
        PRefs.parent.push (StoryTopics);

places all topics that occur in myStory in the array StoryTopics. You could represent your topics/stories in several different ways, your choice depends on what you want to do with your data.

Peter