Skip to main content
Known Participant
July 16, 2023
Question

Need script to change index page numbers

  • July 16, 2023
  • 5 replies
  • 789 views

Hello,

I have a book formatted in InDesign. The index was created and sent to me in a .txt file. I have now had to add two pages, so all the index entries need to be advanced by two. Is there a script that can perform this task? Perhaps this could be done in the .txt file, but I could put it inDesign and work from there.

 

Thanks,

Tom

5 replies

Peter Kahrel
Community Expert
Community Expert
July 17, 2023

> in case when there are some numbers in the text as well? 

 

That's true. But it's always a problem to figure out what the topic separator is. Could be an en-space, two spaces -- which are easy to find. Could also be a comma and a space, which is a problem because that's also the page-number separator. If you know the topic separator there's no need to split the index into two columns, you just take each paragraph from the separator to the end.

 

This particular script doesn't handle abbreviated page ranges very well. For instance, 28-9 incremented by 2 should be 30-31, but comes out as 30-11. So you'd have to unrange ranges, then do the adjustments, and re-range. But that, too, is a problem. For instance, 34-36, 37 -- it's simple enough to unrange this to 34, 35, 36, 37, then increment by 2 to 36, 37, 38, 39, but then when you're re-ranging you get 36-39. You don't know that 39 is out of the range.

 

So because there are that many problems with page numbers apart from deciding where they start, and because updating index numbers after the index was produced is an act of desperation, you just hope for the best and keep your fingers crossed. And checking manually later whether there are any numbers in the topic names, by the way, isn't a big deal.

 

What should be added to the script is also a maximum: now it has only one mode: greater than a number. It should also handle greater or smaller than a number.

Robert at ID-Tasker
Legend
July 17, 2023

You are right - as always.

 

I was thinking only about TAB as being a separator - but as you've pointed out - it may be something else.

Then there are different styles of presenting page numbers...

 

Peter Kahrel
Community Expert
Community Expert
July 17, 2023

Here a script to adjust numbers. You need to set the breakOff (the number from which to renumber) and by how much the numbers should be incremented (the value at diff). You can use a negative number to decrement page numbers. To do all numbers, use a break-off of 1.

 

Select a text frame or an insertion point to target a story.

 

 

(function () {
  var breakOff = 100;
  var diff = 2;
  var num;

  try {
    var story = app.selection[0].parentStory;
  } catch (_) {
    alert ('Select a text frame or a text object');
    exit();
  }

  app.findGrepPreferences = null;
  app.findGrepPreferences.findWhat = '\\d+';
  var pNums = story.findGrep();
  for (var i = pNums.length-1; i >= 0; i--) {
    num = Number (pNums[i].contents);
    if (num >= breakOff) {
      pNums[i].contents = String (num + diff);
    }
  }
}());

 

 

Robert at ID-Tasker
Legend
July 17, 2023

Nice, but I think index should be converted into two column table first and then script should work only on a second column?

In case when there are some numbers in the text as well? 

 

Peter Kahrel
Community Expert
Community Expert
July 17, 2023

All page numbers or only those greater than a certain value?

Known Participant
July 17, 2023

Hi Peter,

In my case starting at page 1 for the first volume but page 950 or so for the second volume. I was thinking of asking you to write such a script. I actually did many years ago and it worked beautifully. But change of computers and I lost it. And I have not written scripts for a long time. However, the person doing the indexing has software that will automatically do this work so I now know I no longer need a script.

 

I am wondering, Robert, if you can give some hints as to how to go about changing page numbers using Excel. I do not need it in this instance but perhaps others on this forum would like to know.

 

Thanks everyone,

Tom

Robert at ID-Tasker
Legend
July 17, 2023

As there is always TAB after the text - you can split it in to two columns.

 

Then you would need to split 2nd column further - first by separators - "," - then ranges - "-" - so you can reverse it. 

 

When you have everything separated - you can update page numbers - either via macro - or "=if(...)". 

 

Then join everything back. 

 

The only downside - it will work directly only on 0-9.

With extra tinkering - Romans and letters can be handled as well. 

 

Barb Binder
Community Expert
Community Expert
July 16, 2023

Hi Tom:

 

For next book, it may be worth noting that InDesign can create an index for you. After you add the index markers, you can update it after edits and all the page numbers will update. https://helpx.adobe.com/indesign/using/creating-index.html

 

~Barb

~Barb at Rocky Mountain Training
Robert at ID-Tasker
Legend
July 16, 2023

You can do it in Excel or similar.