Update Index

When you edit an index, for example to combine entries, you can end up with un-well-formed page ranges such as 2, 16-34, 4-8, 5-10, x, 12-18, ii-iv. The Update Index...

These scripts are part of the Indexes and Concordances collection. See the full collection here

The script can be used to apply page-number spans (1, 2, 3 > 1–3), to undo those spans (1–3 > 1, 2, 3), to re-sort page numbers after you edited a topic, and to combine identical topics in a single entry. In the latter case the page references are combined and ranged as well. But first a word about topic separators.

Topic separators

It’s not always easy to determine where the topic ends and where the page references start. Therefore you’ll have to tell the script what the topic separator in your index is. For example, if the topic separator in your index is an en-space, then you set this in the script. Open the script in a text editor and go to this line (it’s at the top of the script):

var topic_separator = '\u2002';

Here \u2002 is the code for the en-space. Naturally, the en-space mustn’t be used for anything else. (Instead of entering \u2002 you can copy and paste the en-space as a character between the single quotes.) After making the change, save the script, making sure that it remains a plain text file.

A popular separator is comma+space, but commas can occur inside a topic, and comma+space is also the standard page-number separator. So comma+space is not a safe separator. Two spaces, too, are unsafe. So if you use a separator that occurs elsewhere as well, you’ll have to change it temporarily.

If you update just a small number of entries there’s no problem changing the separator manually. But if you use the script on a whole index, or a large part of it, you don’t want to replace the separator manually. Instead, use the script index-update-insert-topic-separators.jsx to insert the code %% where it thinks the page references start. The codes are highlighted so that it’s easy to see where they are. Check whether the codes are placed correctly, and if they are, make sure the code is set in the script (%% is the script’s preset, but do check), then select a frame and run the script.

When you’re done editing the index, simply replace the separator code string with the original separator.

Some examples

The script can be used to apply page-number spans. Before:

Leech, G.,%%ii, iii, iv, 1, 2, 3, 7, 10, 21, 22, 23

After:

Leech, G.,%%ii–iv, 1–3, 7, 10, 21–23

Conversely, the script can be used to undo number-spanning (see Page ranges, below).

Another scenario, which occurs often after some robust editing of the index, is that you end up with two identical topics with different page numbers. The page numbers at both topics are correct so you want to merge the two entries. You start with e.g. the following two entries:

Leech, G.,%%ii, iv, 1, 2, 3, 7, 10, 21, 22, 23
Leech, G.,%%iii, vi, 14, 6, 8, 9, 10, 15, 11, 22, 23

Select the two paragraphs (no need to select the full paragraph: you can select from the centre of the first to the centre of the second paragraph), then run the script. The result:

Leech, G.,%%ii–iv, vi, 1–3, 6–11, 14–15, 21–3

Use

The script runs either on just the selected paragraph (no need to select the whole paragraph, just place the cursor somewhere in it), on a number of selected paragraphs that have the same topic (see the example, above), or on the whole index. To process the whole index, select one of the index’s text frames and run the script.

Ranging pages

In the example above, pages are ranged. Whether this should happen and, if yes, in what way, is determined by the setting of the page_span variable at the beginning of the script. “0” means ‘no ranging at all’; “1” is the standard ranging of consecutive numbers; “2” allows for gaps of one page. By increasing the value of page_span, the spanner becomes more tolerant. Look for this line at the beginning of the script:

page_span = 1

and set the value you want (0 to disable spanning). Some examples:

page_span = 0: '1, 2, 3, 4, 6, 8' (no change)
page_span = 1: '1, 2, 3, 4, 6, 8' > '1-4, 6, 8'
page_span = 2: '1, 2, 3, 4, 6, 8' > '1-6, 8'
page_span = 3: '1, 2, 3, 4, 6, 8' > '1-8'
etc.

Note that you can undo ranging by setting the page span to 0.

Dropping digits (abbreviating ranges)

The script is set to drop digits (also known as ‘abbreviating page ranges’) so that 123-129 is changed to 123-9. Ranges are abbreviated maximally (as is customary in Britain), e.g. 2006-9. To disable digit dropping, look for this line:

drop_digits = true;

and change it to

drop_digits = false;

The script has no interface.

Topic separators

It’s not always easy to determine where the topic ends and where the page references start, in other words, the topic separator. The script’s default is a single en-space, so if you use that as the separator you needn’t change this.

To change the topic separator in the script, look for this line:

var topic_separator = '\u2002';

Here \u2002 is the code for the en-space. If your index uses two spaces, simply replace \u2002 with two spaces. If there’s a comma before the two spaces, include that as well.

But it can become complicated. A popular separator is comma+space, but commas can occur inside a topic, and comma+space is also the standard page-number separator. So comma+space is not a safe topic separator. Therefore, if the index uses a separator that occurs elsewhere as well, you’ll have to change it temporarily to some tag.

If you update just a small number of entries there’s no problem changing the separator manually. But if you use the script on a whole index, or a large part of it, you don’t want to replace the separator manually. Instead, use the Index Update: Insert Topic Separators script (index-update-insert-topic-separators.jsx) to insert the code %% where it thinks the page references start. The codes are highlighted so that it’s easy to see where they are. Check whether the codes are placed correctly, and if they are, make sure the code is set in the script and save it.

Run the script. Then when you’re done editing the index, simply replace the separator code string with the original separator.


Version history

24 Aug. 2023: Rewrote part of the code to make the script more efficient. Added functionality so that you can select any number of paragraphs to merge entries.

October 2015: yet another fix needed for paragraph styles.

June 2010: fixed issue with paragraph styles.


Peter Kahrel Script Repository
Hosted as a community service, and with deep appreciation, by CreativePro.com


Scripts by Topic | Script Index
Basic Script Installation and Use
How to Modify or Configure These Scripts
Resources for Scripting and Publishing Automation
About This Repository


Repository Home Page


Contact Peter Kahrel


Note to users: You run scripts at your own risk: CreativePro and the author accept no liability for anything that may be caused by any of these scripts. Always take the necessary precautions. We encourage you to save backups and test scripts on copies of your documents.

Bookmark
Please login to bookmark Close

This article was last modified on August 22, 2026

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading comments...