10 Replies Latest reply on Feb 28, 2008 2:11 AM by Jeff Sese

    [IDCS2 JS] Adding Page References to Topics Within A Book

    Jeff Sese
      Hi,

      I'm using CS2 JS, and I'm trying to add page references to my index topics.

      Here is my code:

      var myDoc = app.activeDocument;
      var myStory = myDoc.textFrames.item("IndexStory").parentStory;
      var index = myStory.associatedXMLElements;

      if (myDoc.indexes.length == 0) {
      myDoc.indexes.add();
      }

      for (var i=0; i<index.xmlElements.length;i++) {
      var category = index.xmlElements[i];
      for (var j=0; j<category.xmlElements.length; j++) {
      var element = category.xmlElements[j];
      var topicLvl_1;
      if (element.markupTag.name == "ati:category.head") {
      myDoc.indexes[0].topics.add(element.contents, i.toString());
      topicLvl_1 = myDoc.indexes[0].topics[i];
      } else {
      topicLvl_1.topics.add(element.contents);
      markCompany(i, element.contents);
      }
      }
      }

      myDoc.indexes[0].update();

      function markCompany(sector, companyName) {
      app.findPreferences = null;
      app.changePreferences = null;

      var results = app.search(companyName, true, true, undefined);
      for (var i=results.length - 1; i > -1; i--) {
      if (results[i].parentTextFrames.length > 0 &&
      results[i].parentTextFrames[0].parent.appliedMaster != null &&
      (results[i].appliedParagraphStyle.name == "Company Name" ||
      results[i].appliedParagraphStyle.name == "Company Headline left just." ||
      results[i].appliedParagraphStyle.name == "Company Headline right just.")) {
      test = myDoc.indexes[0].topics[sector];
      test2 = test.topics.item(companyName);
      //InDesign Restarts here
      test2.pageReferences.add(results[i], PageReferenceType.currentPage);
      }
      }
      }

      on the line after the comment, when ESTK executes that line, InDesign crashes. I'm using a OS X Tiger.

      Is this has something to do with the scope of my search? cause when i'm only searching the active document, this does not happen. But I have to search all documents that belong to the book.

      Thanks,
      Jeff
        • 1. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
          Peter Kahrel Adobe Community Professional & MVP
          I vaguely remember some problems with app.search(), as with app.documents.everyItem().search(). Well, searching was no problem but doing things with what you found like adding page references seemed to be. I therefore always cycle through documents. Maybe a bit more overhead, but never a problem. Maybe you could give that a shot.

          Peter
          • 2. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
            Jeff Sese Level 1
            Hi Peter,

            Thanks, I tried what you suggested and used a for loop to iterate through app.documents and doing the search per document, but still InDesign keeps on crashing. Is this some sort of bug? Just like the one reported in CS3?

            Is there any work-around in doing this? I need to add page references in topics and the words might appear in multi-documents inside a book. I'm considering making the book into one big document but it is fairly large, around 300 pages. It will likely slow down my machine.

            In the UI there is this add all button when creating page references, is this accessible via scripting? Or is the only solution via search?

            Thanks,
            Jeff
            • 3. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
              Peter Kahrel Adobe Community Professional & MVP
              Jeff,

              I never have InDesign crashing on me when I add topics/references using search on a document-by-document basis (CS2 and CS3), so I don't think it's an InDesign bug. Maybe it has something to do with your documents. I'm pretty sure that combining the documents into one isn't going to help at all.

              Can you do the page references on a single document? If yes, try all of them separately: it could be just one document that's giving you trouble.

              As to the "add all" button, no that's not available in scripting.

              Peter
              • 4. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                Jeff Sese Level 1
                Hi Peter,

                I did a step-by-step debugging and it shows that InDesign crashes when the search result is not in the document that has the index (the index is only in one document of the book).

                Inserting a page reference works for all of the documents individually. When I'm processing it individually, my search is done using:

                myDoc = app.activeDocument;
                myDoc.search();

                But when I try do the whole book, I go inside a loop:

                for (var i=0; i<app.documents.length; i++) {
                myDoc = app.documents[i];
                myDoc.search();
                }

                Search returns the text objects fine. But when it's time to add a page reference and the found object is in not in the document where the index was created, then it crashes.

                Like if i have 2 documents, I created the index in document 2. If a topic is found to be in both documents, then topics found in document 2 can be inserted with a page reference without problems. But the topics found in document 1, when I try to insert a page reference InDesign crashes.

                This is really frustrating because I have no clue whatsoever on why this is happening.

                Thanks for all the help. I just wish that I can resolve this.

                Thanks again,
                Jeff
                • 5. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                  Jeff Sese Level 1
                  Here is the some of the error details that the MAC gives out:

                  Version: 4.0.5.688 (4050)

                  PID: 2500
                  Thread: Unknown

                  Exception: EXC_BAD_ACCESS (0x0001)
                  Codes: KERN_PROTECTION_FAILURE (0x0002) at 0x00000000

                  I hope this helps.

                  Thanks,
                  Jeff
                  • 6. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                    Peter Kahrel Adobe Community Professional & MVP
                    Your script works fine when you run it on a single document because you create a new index in it. But when you run it against a book, it looks as if you're trying to have the topics in the index in one document point to page references in other documents. That doesn't work and is probably what goes wrong (though it shouldn't crash InDesign). You need to create an index in each document. Later, when you generate the index, all indexes are combined.

                    As to avoiding crashes mishaps when scripting indexes, place all or part of your script in a try-catch statement (this is a good precaution in general). I always use pageReferences.add() in a try-catch construction because it can fail on special characters (among others) and in CS2 it fails in footnotes.

                    Peter
                    • 7. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                      Peter Kahrel Adobe Community Professional & MVP
                      'Fraid those error codes don't mean much to me.
                      • 8. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                        Jeff Sese Level 1
                        Hi Peter,

                        Do all my index topics have to look the same? The topic levels? Or I can just create topics for each document?

                        I did place the problematic code in a try-catch block, but still it crashes.

                        I'll gonna try this one out and report back any results.

                        Thanks for the quick response.

                        Jeff
                        • 9. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                          Peter Kahrel Adobe Community Professional & MVP
                          >Do all my index topics have to look the same? The topic levels? Or I can just create topics for each document?

                          You have to create topics in each document. If they're not exactly the same then you end up with different entries in your final index. But when you add them by script they should be the same, right?

                          Peter
                          • 10. Re: [IDCS2 JS] Adding Page References to Topics Within A Book
                            Jeff Sese Level 1
                            Hi Peter,

                            Adding indexes per document solved the problem, thanks a lot.

                            I'm adding the topics via script, but I did some extra steps cause the topics are based from the XML structure in one of the book documents. Each document has a different structure so I must preserve the topics from the source documents that has the correct XML structure.

                            Again thanks,
                            Jeff