8 Replies Latest reply on May 27, 2017 5:20 AM by Oriup

    Need Script to add page number to index cross reference

    Kartman

      I am doing a 900 page medical reference book. Because this is a reference book, the index is a real critical piece of this. Right now the index alone is 38 pages long. When I have InDesign generate the index for each cross reference, it just lists the name of the cross reference.

      The author originally marked the synonyms to be cross references which I tagged as cross references in the index.

      For example it now says "Eagle-Barrett syndrome. See prune belly". The reader would have to flip to the prune belly index entry where it would say "Prune belly 408", then would then have to go to page 408.

      The author would now like to also say the page number next to the cross references so it would say ""Eagle-Barrett syndrome. See prune belly on page 408".

      Could anyone:

      A:Confirm this is something a script could do.

      B: Give many any direction on a script that may do this or do something similar.

       

      Thank you in advance.

        • 1. Re: Need Script to add page number to index cross reference
          Peter Kahrel Adobe Community Professional & MVP

          This is not too hard to script. I'll send you something later.

           

          Peter

          • 2. Re: Need Script to add page number to index cross reference
            Peter Kahrel Adobe Community Professional & MVP

            Kartman,

             

            The easiest and most secure way to tackle this is to take three steps, using two scripts.

             

            1. Script 1 marks all cross-references and their cross-referenced topics in the Index panel.

            2. Generate the index (or update it if you had created the index earlier)

            3. Place the cursor somewhere in the new index and run script 2.

             

            If you use a book file, you need to run script 1 in each chapter separately.

             

             

            Script 1: Mark each cross-reference and its cross-referenced topic by suffixing it with %#%

             

            var topics = app.documents[0].indexes[0].allTopics;
            var xrefs;
            for (var i = topics.length-1; i >= 0; i--) {
                if (topics[i].isValid) {
                    xrefs = topics[i].crossReferences;
                    for (var j = xrefs.length-1; j >= 0; j--) {
                        if (xrefs[j].crossReferenceType == CrossReferenceType.SEE) {
                            xrefs[j].referencedTopic.name += '%#%';
                        }
                    }
                }
            }
            

             

             

            Script 2: After generating the index, place the cursor somewhere in the index and run this script.

             

            app.findGrepPreferences = app.findTextPreferences = null;
            var story = app.selection[0].parentStory;
            app.findGrepPreferences.findWhat = '\\bSee\\s.+?%#%';
            var xreffedTopic;
            var pageRefs;
            var xrefs = story.findGrep();
            for (var i = xrefs.length-1; i >= 0; i--) {
                app.findTextPreferences.findWhat = '^p' + xrefs[i].contents.replace (/^See\s/, "");
                xreffedTopic = story.findText();
                if (xreffedTopic.length == 1) {
                    pageRefs = xreffedTopic[0].paragraphs[1].contents.replace (/^.+?%#%/, "");
                } else {
                    pageRefs = '????';
                }
                
                xrefs[i].insertionPoints[-1].appliedCharacterStyle = app.documents[0].characterStyles[0];
                xrefs[i].insertionPoints[-1].contents = ' on page' + pageRefs;
            }
            
            app.findGrepPreferences = app.changeGrepPreferences = null;
            app.findGrepPreferences.findWhat = '%#%';
            story.changeGrep();
            
            • 3. Re: Need Script to add page number to index cross reference
              Kartman Level 1

              Great! Thank you so much. I will give it a try tonight to see how it works.

              It is set up like a book into 16 documents, so I will have to run script 1 on each chapter.

              • 4. Re: Need Script to add page number to index cross reference
                Kartman Level 1

                I applied your script and found a few minor errors.

                A)It would be nice to have a notification that the first script ran. I did know it had run, and ran it again, and it dumped a bunch of extra %#% into the index items.

                B) For the "See " cross reference functions it added and extra carriage return to the end of the line. It also put two spaces before the page number. I fixed these by doing a find/replace with the appropriate characters. (over two hundred instances each).

                C) It did not do the "See Also" cross references. The script did not add the page numbers correctly. It said "See also topic on page????".

                 

                Thank you very much of your help.

                 

                 

                • 5. Re: Re: Need Script to add page number to index cross reference
                  Peter Kahrel Adobe Community Professional & MVP

                  A) An amended version is below. It now adds the tag only if it isn't there already so that you can run the script again, e.g. after adding new topics and cross-references.

                   

                  B) HAdn't spotted that in my small sample. It's fixed in the revised script, below.

                   

                  C) That's because you hadn't asked for that. Added in the revision, below.

                   

                  Peter

                   

                   

                  Script 1

                   

                  var topics = app.documents[0].indexes[0].allTopics;
                  var xrefs;
                  for (var i = topics.length-1; i >= 0; i--) {
                      if (topics[i].isValid) {
                          xrefs = topics[i].crossReferences;
                          for (var j = xrefs.length-1; j >= 0; j--) {
                              if (xrefs[j].crossReferenceType == CrossReferenceType.SEE || xrefs[j].crossReferenceType == CrossReferenceType.SEE_ALSO) {
                                  if (xrefs[j].referencedTopic.name.indexOf('%#%') < 0) {
                                      xrefs[j].referencedTopic.name += '%#%';
                                  }
                              }
                          }
                      }
                  }
                  

                   

                  Script 2

                   

                  app.findGrepPreferences = app.findTextPreferences = null;
                  var story = app.selection[0].parentStory;
                  app.findGrepPreferences.findWhat = '\\bSee\\s.+?%#%';
                  var xreffedTopic;
                  var pageRefs;
                  var xrefs = story.findGrep();
                  for (var i = xrefs.length-1; i >= 0; i--) {
                      app.findTextPreferences.findWhat = '^p' + xrefs[i].contents.replace (/^See(\salso)?\s/, "");
                      xreffedTopic = story.findText();
                      if (xreffedTopic.length == 1) {
                          pageRefs = xreffedTopic[0].paragraphs[1].contents.replace (/^.+?%#%/, "").replace ('\r', "");
                      } else {
                          pageRefs = '????';
                      }
                      
                      xrefs[i].insertionPoints[-1].appliedCharacterStyle = app.documents[0].characterStyles[0];
                      xrefs[i].insertionPoints[-1].contents = ' on page' + pageRefs;
                  }
                  
                  app.findGrepPreferences = app.changeGrepPreferences = null;
                  app.findGrepPreferences.findWhat = '%#%';
                  story.changeGrep();
                  
                  • 6. Re: Re: Re: Need Script to add page number to index cross reference
                    Kartman Level 1

                    This is great. These seems to work great.

                    However on three of my chapters/documents it crashes InDesign when I run Script 1. It runs fine on my other 13 chapters/documents.

                    How can I diagnose what the problem is?

                    I get the problem with InDesign report which is sent to Adobe. I have a copy of the text that is sent in the report, but I don't know how to interpret it.

                     

                    Here is the first few several line which I think may be the culprits.

                    Process:         Adobe InDesign CC 2014 [10858]

                    Path:            /Applications/Adobe InDesign CC 2014/Adobe InDesign CC 2014.app/Contents/MacOS/Adobe InDesign CC 2014

                    Identifier:      com.adobe.InDesign

                    Version:         10.0.0.70 (10000)

                    Code Type:       X86-64 (Native)

                    Parent Process:  launchd [251]

                    Responsible:     Adobe InDesign CC 2014 [10858]

                    User ID:         507

                     

                    Date/Time:       2014-09-05 05:18:42.330 -0600

                    OS Version:      Mac OS X 10.9.4 (13E28)

                    Report Version:  11

                    Anonymous UUID:  78B5BB07-86AE-BD5E-A353-770D3D985798

                     

                    Sleep/Wake UUID: DE9B7373-55EB-4C1B-981E-33A82E95D3E0

                     

                    Crashed Thread:  0  Main Thread  Dispatch queue: com.apple.main-thread

                     

                    Exception Type:  EXC_BAD_ACCESS (SIGSEGV)

                    Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000008

                     

                    VM Regions Near 0x8:

                    -->

                        __TEXT                 000000010eda7000-000000010edad000 [   24K] r-x/rwx SM=COW  /Applications/Adobe InDesign CC 2014/Adobe InDesign CC 2014.app/Contents/MacOS/Adobe InDesign CC 2014

                     

                    Thread 0 Crashed:: Main Thread  Dispatch queue: com.apple.main-thread

                    0   com.adobe.InDesign.Indexing   0x000000011f07358a 0x11f002000 + 464266

                    1   com.adobe.InDesign.Indexing   0x000000011f051e72 0x11f002000 + 327282

                    2   com.adobe.InDesign.Indexing   0x000000011f051cfd 0x11f002000 + 326909

                    3   PublicLib.dylib               0x000000011008c77e CScriptProvider::AccessPropertyOnObjects(IDType<ScriptID_tag>, IScriptRequestData*, adobe::version_1::vector<InterfacePtr<IScript>, adobe::version_1::capture_allocator<InterfacePtr<IScript> > > const&) + 494

                    4   com.adobe.InDesign.Scripting  0x000000011db96447 0x11db65000 + 201799

                    5   com.adobe.InDesign.Scripting  0x000000011db92b63 0x11db65000 + 187235

                    6   com.adobe.InDesign.Scripting  0x000000011db93423 0x11db65000 + 189475

                    7   com.adobe.InDesign.Support for JavaScript 0x000000011d4c0b33 0x11d4b7000 + 39731

                    8   com.adobe.AdobeExtendScript   0x0000000119c9d86d ScScript::InitTerm::atExit(void (*)()) + 32925

                    9   com.adobe.AdobeExtendScript   0x0000000119c5b9f6 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 57558

                    10  com.adobe.AdobeExtendScript   0x0000000119c568cb ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 36779

                    11  com.adobe.AdobeExtendScript   0x0000000119c5686d ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 36685

                    12  com.adobe.AdobeExtendScript   0x0000000119c568b4 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 36756

                    13  com.adobe.AdobeExtendScript   0x0000000119c5686d ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 36685

                    14  com.adobe.AdobeExtendScript   0x0000000119c504e1 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 11201

                    15  com.adobe.AdobeExtendScript   0x0000000119c4f088 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 5992

                    16  com.adobe.AdobeExtendScript   0x0000000119c53b9b ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 25211

                    17  com.adobe.AdobeExtendScript   0x0000000119c58c25 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 45829

                    18  com.adobe.AdobeExtendScript   0x0000000119c53bdd ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 25277

                    19  com.adobe.AdobeExtendScript   0x0000000119c58c25 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 45829

                    20  com.adobe.AdobeExtendScript   0x0000000119c51bfd ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 17117

                    21  com.adobe.AdobeExtendScript   0x0000000119c58c25 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 45829

                    22  com.adobe.AdobeExtendScript   0x0000000119c53bdd ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 25277

                    23  com.adobe.AdobeExtendScript   0x0000000119c58c25 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 45829

                    24  com.adobe.AdobeExtendScript   0x0000000119c51bfd ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 17117

                    25  com.adobe.AdobeExtendScript   0x0000000119c58c25 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 45829

                    26  com.adobe.AdobeExtendScript   0x0000000119c52d23 ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 21507

                    27  com.adobe.AdobeExtendScript   0x0000000119c7f36a ScScript::ScopeNode::dump(ScScript::Engine&, int) + 62378

                    28  com.adobe.AdobeExtendScript   0x0000000119c6a8be ScScript::FileDisp::createFile(ScCore::Variant&, ScCore::String const&) + 118686

                    29  com.adobe.AdobeExtendScript   0x0000000119ca1ffb ScScript::InitTerm::atExit(void (*)()) + 51243

                    30  com.adobe.AdobeExtendScript   0x0000000119ca3de6 ScScript::InitTerm::atExit(void (*)()) + 58902

                    31  com.adobe.AdobeExtendScript   0x0000000119ca3ab1 ScScript::InitTerm::atExit(void (*)()) + 58081

                    32  com.adobe.InDesign.Support for JavaScript 0x000000011d4c688f 0x11d4b7000 + 63631

                    33  com.adobe.InDesign.Support for JavaScript 0x000000011d4c62a7 0x11d4b7000 + 62119

                    34  com.adobe.InDesign.Support for JavaScript 0x000000011d4c52e9 0x11d4b7000 + 58089

                    35  com.adobe.InDesign.Scripts Panel 0x0000000121cef2a1 0x121ced000 + 8865

                    36  com.adobe.InDesign.Scripts Panel 0x0000000121cf3499 0x121ced000 + 25753

                    37  com.adobe.InDesign.Actions    0x00000001207d4ae8 0x1207be000 + 92904

                    38  com.adobe.InDesign.Actions    0x00000001207db350 0x1207be000 + 119632

                    39  com.adobe.InDesign.Actions    0x00000001207f8a99 0x1207be000 + 240281

                    40  com.adobe.InDesign.Actions    0x00000001207dbe5e 0x1207be000 + 122462

                    41  com.adobe.InDesign.Application UI 0x000000011939586c 0x119264000 + 1251436

                    42  com.adobe.InDesign.Application UI 0x00000001193946e4 0x119264000 + 1246948

                     

                    Thank You

                    • 7. Re: Need Script to add page number to index cross reference
                      Peter Kahrel Adobe Community Professional & MVP

                      I've no idea how to diagnose that. Can you send you smallest failing document (zipped) to me at kahrel@kahrel.plus.com? Then I'll have a look.

                       

                      P.

                      • 8. Re: Need Script to add page number to index cross reference
                        Oriup Level 1

                        Kartman,

                         

                        You should mark correct answer on Peter post.

                         

                        Sumit

                        1 person found this helpful