17 Replies Latest reply on Jun 20, 2013 5:15 AM by [Jongware]

    Change Page Numbers in Indexes

    odynenydo

      Hello,

       

      I have a catalog and I have added two pages in the middle of the catalog. Now, because of this two page addition, I need to update the page numbers listed on the indexes with the new page numbers. I only need to increase each of the page numbers by two, but that only starts on a certain point. So, example, on one of the indexes I have, the page numbers 1-70 can remain the same, since the two extra pages were added AFTER page 70. But every number after 70 needs to increase by 2.

       

      I have a find and replace script. I create a table in excel and drop into the document. The script then takes whatever word or number that is listed in Column A and replaces it with what is in Column B. I originally used this script to find and replace product codes in the catalog, and it worked great because all of the product codes were completely unique.

       

      Trying to use this same script to change the page numbers, but it totally backfired. The issue is that the same number will reoccur later in the table, so the script has the table constantly replacing all of the numbers with the first two. So, here's a small sample of my table:

       

      Col A      Col B

      423         425

      422         424

      421         423

      420         422

       

      But when I run the script, here's what it will turn into:

       

      Col A                            Col B

      423 becomes 425         425

      422 becomes 424         424

      421 becomes 425         423 becomes 425

      420 becomes 424         422 becomes 424

       

      Because it changes the table, all of the corresponding numbers in the document will then change to 425 or 424.

       

      Is there a way to make it so that the script does not change the information in the table that it uses as a reference?

       

      Here is the script below. I did not write it (some very nice person on these forums did) and don't know anything about scripting.

       

      the_table = app.selection[0].tables[0];
      app.findChangeTextOptions = null;
      with (app.findChangeTextOptions)
      {
      caseSensitive = true;
      wholeWord = true;
      }
      app.findTextPreferences = null;
      app.changeTextPreferences = null;
      for (row=0; row<the_table.rows.length; row++)
      {
         if (the_table.rows[row].cells[0].contents == '')
           continue;
         app.findTextPreferences.findWhat = the_table.rows[row].cells[0].contents;
         app.changeTextPreferences.changeTo = the_table.rows[row].cells[1].contents;
         app.activeDocument.changeText();
      }

       

       

       

      Thank so much!

        • 1. Re: Change Page Numbers in Indexes
          [Jongware] Most Valuable Participant

          Maybe it's easier just to write a script that increment only the numbers you need by 2, inside a text selection (you'd select the full index first, to prevent updating *every* number in your entire document).

           

          What do they look like, now? Was this a standard InDesign index, with comma+space separated numbers?

          • 2. Re: Change Page Numbers in Indexes
            odynenydo Level 1

            Well, I have 3 different indexes I need to update. Two of them are the standard comma+space, the other just lists individual page numbers. Here are samples two of the three indexes:

             

            index1.jpg

             

            index2.jpg

             

            These indexes were originally created in excel and then imported into Indesign. I could just update and reimport the excel file, but formatting some of these indexes back again would be very time consuming (especially the first one I shown here - that index alone covers thirty pages). So if there is a script that can just change the page numbers in the Indesign file, that would be ideal.

             

            Thanks!

            • 3. Re: Change Page Numbers in Indexes
              Maria964 Level 1

              Hi, Jonqware!

               

              I think your proposal is the best one!

               

              This problems happens a lot with me and I really whould like to have a script that increses numbers by a defined amount defined inside a text selection.

               

              Can any of the scripting gurus provide that for a spanhish non coder?

               

              Thank you in advance.

               

              Maria

              • 4. Re: Change Page Numbers in Indexes
                [Jongware] Most Valuable Participant

                This should work, but make sure you make a backup copy first. I'm only going to say this once: make a backup copy first. Yes, I said you should make a backup copy first!

                 

                Copy the script below, paste into a suitable editor -- Adobe's ESTK that comes with InDesign is good enough. Save as "omgwrongnumbers.jsx" into your User Scripts folder. Select as little text as possible, the script will blindly increment (or decrement) all numbers in the range. Then double-click the script to run.

                 

                //DESCRIPTION:omg the page numbers are all wrong!
                // A Jongware Script 18-Aug-2010
                if (app.documents.length == 0)
                {
                     alert ("Oh give me some text to play with :'(");
                     exit(0);
                }
                if (app.selection.length != 1)
                {
                     alert ("We can't go on like this. Select some text first.");
                     exit(0);
                }
                
                myDialog = app.dialogs.add ({name:"omg the numbers are wrong!",canCancel:true});
                
                with (myDialog)
                {
                     with (dialogColumns.add())
                     {
                          with (dialogRows.add())
                               staticTexts.add ({staticLabel:"First to change"});
                          with (dialogRows.add())
                               aBox = integerEditboxes.add({editContents:"1"});
                          with (dialogRows.add())
                               staticTexts.add ({staticLabel:"Last to change"});
                          with (dialogRows.add())
                               bBox = integerEditboxes.add({editContents:"99999"});
                          with (dialogRows.add())
                               staticTexts.add ({staticLabel:"Add or subtract this value"});
                          with (dialogRows.add())
                               cBox = integerEditboxes.add({editContents:"2"});
                     }
                }
                if (!myDialog.show())
                {
                     myDialog.destroy();
                     exit(0);
                }
                first = aBox.editValue;
                last = bBox.editValue;
                step = cBox.editValue;
                
                if (first < 1 || first > last || step == 0)
                {
                     alert ("Now you're pulling my nose arentya");
                     exit(0);
                }
                app.findGrepPreferences = null;
                app.findGrepPreferences.findWhat = "\\b\\d+\\b";
                list = app.selection[0].findGrep(true);
                changes = 0;
                for (i=0; i<list.length; i++)
                {
                     n = Number(list[i].contents);
                     if (n >= first && n <= last)
                          changes++, list[i].contents = String(n+step);
                }
                alert ("Number of changes: "+changes);
                 
                

                1 person found this helpful
                • 5. Re: Change Page Numbers in Indexes
                  [Jongware] Most Valuable Participant

                  Oh I should probably state that it works normally on either a normal text selection or on a table selection. It does not work on text in PDFs, cross-references, automatically numbered lists, or page number codes. It doesn't "add 10% to all prices in the range". It does not adjust roman numbers. It will not function correctly with floating point numbers, and refuses to run on negative numbers. This is meant for simple indexes only.

                  • 6. Re: Change Page Numbers in Indexes
                    Maria964 Level 1

                    Hi.

                     

                    Thank you very much, Jongware.

                    The next time a client say "we have to add some pages to the final job and change the index" I will look at him and will say: "Sure. No problema"....
                    This is the third problem you solve for me. I owe you one more.
                    Best regards and if you ever need anything from Spain, just tell me.
                    Maria

                    • 7. Re: Change Page Numbers in Indexes
                      odynenydo Level 1

                      This worked perfectly! It was exactly the script I was looking for. Thanks so much, you're awesome!

                      • 8. Re: Change Page Numbers in Indexes
                        mfmiller3000 Level 1

                        Thanks so much for posting your script Jongware - I was able to adapt it for my own index renumbering need and it worked perfectly.

                        • 9. Re: Change Page Numbers in Indexes
                          [Jongware] Most Valuable Participant

                          Thank you! It's funny, this is a 5 year old script and I can not recollect writing it. But it sure feels good it's still helping people out.

                           

                          (As it happens, I really needed a moral boost right now. Thanks!)

                          • 10. Re: Change Page Numbers in Indexes
                            Harbs. Level 6

                            Hey Theun,

                             

                            I know it's been a looong year, but 5 years?!? Wow!

                            • 11. Re: Change Page Numbers in Indexes
                              wjnp2000 Level 2

                              Hey!

                               

                              Let me take a ride a acrescent a question:

                               

                              I see that works fine but, in my case, sometimes the text are not in columns on a table. The numbers are tabulated like this:

                              1.<tab>Text<tab><tab><number>

                              See picture below:

                              Screen shot 2011-11-29 at 09.41.33.png

                              There is way to change these page numbers?

                              I work with ID CS 5.5 (7.5.1 yet) on Mac OS 10.6.8.

                               

                              Thanks in advance!

                              Wagner

                              • 12. Re: Change Page Numbers in Indexes
                                [Jongware] Most Valuable Participant

                                The script changes each and every single number, so in your case it would also change the article numbers. Since your text is not an index (where a sequence of numbers such as "1, 4, 8" could occur) but a table of contents instead, all numbers to change are at the end of a line.

                                 

                                Try with this change. Replace this line near the bottom of the script

                                 

                                app.findGrepPreferences.findWhat = "\\b\\d+\\b";
                                

                                 

                                with this one

                                 

                                app.findGrepPreferences.findWhat = "\\b\\d+$";

                                 

                                and it ought to work.

                                 

                                Notes: Untested. Use At Your Own Risk. Save a Copy First. Check, then Check Again.

                                • 13. Re: Change Page Numbers in Indexes
                                  wjnp2000 Level 2

                                  Thanks Jongware!

                                   

                                  Works perfectly!

                                  I will keep both, to use in TOC and tables.

                                   

                                  Thanks again.

                                  Wagner

                                  • 14. Re: Change Page Numbers in Indexes
                                    KirstenSD

                                    Jongware,

                                     

                                    I know this is a very old script and an old thread, but I wonder if you might be able to help with a variation. I've used your script tons and found it extremely helpful. I have jobs where chapters have been reordered within the book, and I would like to be able to reuse the index by changing multiple page ranges to the new numbers. For example, pages 26-57 have become 66-97, pages 84-109 have become 154-179, etc. The page numbers for each chapter change by a fixed amount, but it's different for each chapter, and obviously some of the new numbers are the same as old ones, so running the script multiple times for the different shifts would not work, as it would pick up new numbers.

                                     

                                    I've done this manually by using character color--searching for each old number in black and changing it to the new number in magenta, so that any numbers that have been changed already are not picked up in subsequent searches. Then when all changes are made, I just change all text back to black. But obviously this is a ridiculously tedious process.

                                     

                                    Is there a way to incorporate this type of solution into the existing script? I'm a non-scripter, so don't have the facility to do this myself.

                                     

                                    I'm using ID CS5.5 on Mac.

                                     

                                    Thanks in advance!

                                     

                                    Kirsten

                                    • 15. Re: Change Page Numbers in Indexes
                                      [Jongware] Most Valuable Participant

                                      Under the line

                                       

                                      app.findGrepPreferences = null;

                                       

                                      add this new line:

                                       

                                      app.findGrepPreferences.fillColor = app.activeDocument.swatches.item("Black");

                                       

                                      and change the 3rd line from the bottom from

                                       

                                      if (n >= first && n <= last)

                                           changes++, list[i].contents = String(n+step);

                                       

                                      to

                                       

                                      if (n >= first && n <= last)

                                                changes++, list[i].properties = {contents:String(n+step), fillColor:app.activeDocument.swatches.item("magenta")};

                                       

                                      -- the "magenta" here needs to be the name of your temporary color swatch. (You cannot create a swatch "Magenta"; it seems to be a reserved name. But whatever, the name of the swatch is irrelevant.)

                                      • 16. Re: Change Page Numbers in Indexes
                                        KirstenSD Level 1

                                        That worked perfectly--thank you so much! You just saved me a ton of time.

                                         

                                        Have a terrific day!

                                        • 17. Re: Change Page Numbers in Indexes
                                          [Jongware] Most Valuable Participant

                                          KirstenSD wrote:

                                           

                                          That worked perfectly--thank you so much! You just saved me a ton of time.

                                           

                                          Have a terrific day!

                                           

                                          Aww shucks. Right now you made it one.