Article View: adobe.scripting.indesign
Article #8617Get (relative) index of a paragraph inside a selection of paragraphs
From: Martin Fischer*
Date: Sun, 01 Oct 2006 03:58
Date: Sun, 01 Oct 2006 03:58
52 lines
3154 bytes
3154 bytes
I have to overwork a bibliography.
If there are more than one entries of one author then the authors name shouldn't be repeated but replaced with a string: "-," (en-dash comma tab).
The entries look like this:
[surename], [forename], [title], [place of publication]: [publisher] [publication
date], [range of pages]
So I have made a script for a selection of entries of the bibliography to replace double or several recurrences of the same [surename] and [forename]. The items to be compared could be defined by a range of text of a paragraph from first index up to the index of the second occurence of a comma (distinct).
This is my approach which works, but it works slowly because it compares more entries than necessary:
var mySel = app.selection[0];
var mySubst = "�,\t";
app.findPreferences = app.changePreferences = null;
for (i = 0; i < mySel.paragraphs.length -2; i++) {
var myFirstStart = mySel.paragraphs[i].insertionPoints[0].index;
var myFirstEnd = mySel.paragraphs[i].search(",", false, false, undefined)[1].index+1;
var myFirstEntry = mySel.parentStory.characters.itemByRange(myFirstStart, myFirstEnd).contents[0];
for (k = mySel.paragraphs.length -1; k > i; k--){
var myNextStart = mySel.paragraphs[k].insertionPoints[0].index;
var myNextEnd = mySel.paragraphs[k].search(",", false, false, undefined)[1].index+1;
var myNextEntryRange = mySel.parentStory.characters.itemByRange(myNextStart, myNextEnd);
if (myNextEntryRange.contents[0] == myFirstEntry)
myNextEntryRange.contents = mySubst;
}
}
The script will compare the authors item of every paragraph of the selection (but the last) with the authors item from the last paragraph of the selection down to the paragraph after the active paragraph of the first loop. The second loop allways starts at the last paragraph of the selection.
The script would be much faster, if the second loop would not start at the last paragraph of the selection but at the paragraph with the last occurence of a searched author's entry.
This last occurence could be found with something like this:
var myResults = mySel.search("\r" + myFirstEntry, false, false, undefined);
var myLastPara = myResults[myResults.length-1].insertionPoints[-1].paragraphs[0];
But how could I find out the index of this paragraph within the selected paragraphs to start the second loop at this index? (Is it the second, the third, the fourth or another paragraph of the selection?)
Thanks
Martin
Technical Details
Message-ID:
<3bc1d60c.-1@webcrossing.la2eafNXanI>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!news.isoc.lu!webcrossingShow Full Headers
Complete Article Headers
X-Google-Thread: f939a,11f901afe18132ac,start X-Google-Attributes: gidf939a,public X-Google-Language: ENGLISH,ASCII From: "Martin Fischer*" <martin.fischer@online.de> Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!news.isoc.lu!webcrossing Newsgroups: adobe.scripting.indesign Subject: Get (relative) index of a paragraph inside a selection of paragraphs Message-ID: <3bc1d60c.-1@webcrossing.la2eafNXanI> Date: Sat, 30 Sep 2006 20:58:01 -0700 Lines: 64 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit