Copy link to clipboard
Copied
I am missing a fundamental concept in InDesign. Given a particular paragraph in a story, I need to navigate backwards paragraph-by-paragraph until I find another paragraph I am looking for. FrameMaker uses linked lists internally, so it is easy:
while (pgf.isValid) {
if (pgf.Name === "Head 1") {
return pgf;
}
pgf = pgf.PrevPgfInFlow;
}
What is the best way to do this with InDesign JavaScript? Thank you very much in advance.
Rick Quatro
The official way is to use http://jongware.mit.edu/idcsjs5.5/pc_Paragraphs.html#previousItem
that is, given a paragraph p, you access the preceding one using
pprev = p.parentStory.paragraphs.previousItem(p);
But it has been observed that this is ex-cru-ci-at-ing-ly s--l--o--w. If you need to do this a couple of times, you might be better off first gathering all paragraphs in a story using everyItem, and use that instead as a quick-indexed array.
Copy link to clipboard
Copied
The official way is to use http://jongware.mit.edu/idcsjs5.5/pc_Paragraphs.html#previousItem
that is, given a paragraph p, you access the preceding one using
pprev = p.parentStory.paragraphs.previousItem(p);
But it has been observed that this is ex-cru-ci-at-ing-ly s--l--o--w. If you need to do this a couple of times, you might be better off first gathering all paragraphs in a story using everyItem, and use that instead as a quick-indexed array.
Copy link to clipboard
Copied
Theunis, that is excellent, thank you very much for the quick response. I hate to abuse your generousity, but could you give me an example of how to do it with everyItem and an array? Thanks again!
Rick
Copy link to clipboard
Copied
That could be something like this (but remove the alerts! they are only there to show how slow the initial Gathering is. (It's faster if There Can Be Only One (Paragraph).)
// this takes a while ...
alert ("wait for it...");
pars = app.selection[0].parentStory.paragraphs.everyItem().getElements();
alert ("gathered "+pars.length+" paragraphs--there we go!");
// but then this is fast!
for (i=0; i<pars.length; i++)
if (pars.contents.indexOf(" the ") >= 0)
alert (pars.contents);
Ye Gods how annoying! The only way to get the Javascript Syntax Highlight button in the Advanced Editor mode is to post, then edit-post! Fume, fume!
.. Oh well. Good thing it's Friday.
Copy link to clipboard
Copied
Yes, I see that collecting the paragraphs ahead of time will make things faster for future searches through the paragraphs. However, I am still stuck on one thing. Given that I have the pars array (or collection), and I have the specific paragraph p, I want to start with p and work backwards through the array. I know how to run the array loop backwards, but I am not sure how to find the index of p in the pars array. Is there a convenient way of doing this? Thanks.
Rick
Copy link to clipboard
Copied
I usually use Ariel's approach (but watch out for wrap around at the beginning of the story), but lately, I've been using paragraph counts by employing this function to get the paragraph count:
function paraCount(story, start, end) {
// start and end should either be the same kinds of text objects within the story
// or indexes in the story
return story.insertionPoints.itemByRange(start, end).texts[0].paragraphs.length;
}
So, the previous paragraph is:
myStory.paragraphs[paraCount(myStory, 0, myPara.index) - 1];
But again, watch out if myPara.index == 0 because that'll give you the same paragraph you started with.
Dave
Copy link to clipboard
Copied
Jongware's idea is good. What I do, though, is as follows, given a
paragraph myPara:
myPreviousPara =
myPara.parentStory.insertionPoints[myPara.insertionPoints[0].index-1].paragraphs[0];
To get the next paragraph, it's even easier:
myNextPara = myPara.insertionPoints[-1].paragraphs[0];
since the last insertionPoint of a paragraph actually belong to the next
paragraph for some reason.
Ariel
PS I haven't done this for a while, and it could be that the value
index-1 above should actually be index-2 because of the fact that the
last insertionPoint belong to the next paragraph, as mentioned.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now