5 Replies Latest reply on Aug 27, 2008 3:18 AM by (Dave_Saunders)

    Placing Index Markers Based on Character Style

    Jeff Sese Level 1
      I have this simple script in IDCS2 that places index markers based of character style:

      myDocument = app.activeDocument;
      app.findPreferences = NothingEnum.nothing;
      app.changePreferences = NothingEnum.nothing;

      if (myDocument.indexes.length == 0) myDocument.indexes.add();

      var index = myDocument.indexes[0];

      var finds = myDocument.search(undefined, undefined, undefined, undefined, {appliedCharacterStyle: "IndexHolder"});

      for (var i=finds.length-1; i>=0; i--) {
      var found = finds[i];
      var myTopic = index.topics.add(found.contents);
      myTopic.pageReferences.add(finds[i], PageReferenceType.currentPage);

      But after running the script some markers are placed off with respect to the search term... why is this happening?

      For example, my term is Beijing, some of the markers are placed after the term:

      Beijing some text some ^more text (marker is placed in the ^ character and not at the start of the word Beijing).

      Hope someone can give me information on why this is happening and how to resolve this. Thanks!

      -- Jeff
        • 1. Re: Placing Index Markers Based on Character Style
          Level 1
          I've seen this. There is a valid reason for putting index markers other than where you try to put them, and it will happen in the UI, too: the reason is to avoid subsequent problems with spelling checking.

          But I think this mechanism goes wrong when a script is placing the index markers.

          I use this function to overcome the problem:
            function secureRefLoc(myIP, actualIP) {
              // To minimize problems with subsequent spelling checking,
              // when a script adds an index reference point, the reference is
              // often inserted a few character distant from the intended location.
              // This function puts the reference where it was intended to be (myIP)
              var myStory = myIP.parent;
              var actualStory = actualIP.parent;
              if (myIP.index != actualIP.index || myStory != actualStory) {
                try {
                  actualStory.characters[actualIP.index].move(LocationOptions.after, myIP);
                } catch(e) {
                  try {
                    actualStory.characters[actualIP.index].move(LocationOptions.before, myIP);
                  } catch(e) {
                    // InDesign thnks we're moving to the original location
          I hope this helps -- you might wonder why I test to see if I'm trying to move to the same location. Well, I don't do that. I always check to see if myIP and myActualIP are the same before calling this function. But the text I'm working on has some mark-up text in it that can result in the the move method rejecting the attempt to move the marker because it thinks the two locations are the same even though they have different indexes.

          Also, on one occasion, the index marker actually showed up in a table when it was supposed to be in a story that held the table, so I had make sure that the parent text flows were attended to by this function.

          I'm sure that there are bugs in the index marker insertion feature of InDesign, but this function works around the bugs, so I haven't pressed hard for these to be fixed.

          • 2. Re: Placing Index Markers Based on Character Style
            Jeff Sese Level 1

            Thanks for the reply.

            To check if I understand it correctly, I'll use this function when my target insertion point (the one where I would like my marker to be placed) and the actual insertion point (the actual index where Indesign placed it) are not equal?

            so if I add my pageReference as:

            myRef = myTopic.pageReferences.add(finds[i], PageReferenceType.currentPage);

            my targetIP = finds[i].insertionPoint[0].

            then I'll test

            if myRef.textSource.index != targetIP

            then call secureRefLoc(myIP, actualIP)

            After this, should I place my marker again?

            -- Jeff
            • 3. Re: Placing Index Markers Based on Character Style
              Level 1
              No, don't place it again. The function moves the marker to where you really wanted it (if it can; in my case, there were sometimes errors, as I explained, and so in those cases, I left it where it was) -- spelling checker be damned!

              When you call the function you need to use your names for the two insertion points.

              • 4. Re: Placing Index Markers Based on Character Style
                Jeff Sese Level 1
                Thanks Dave, it's now working properly.

                I edit my script and now all of the markers are placed correctly. However, in some files, after placing the index markers, my layout moves and now has an overset. I'm thinking, insertion of index markers should not make my text run-off but in some cases these happens. Is this an InDesign CS2 bug? The index marker is a zero-width character right? Does the insertion of an index marker affects how the paragraph composer works? I'm guessing this is the cause of my problem, can anybody confirm this?
                • 5. Re: Placing Index Markers Based on Character Style
                  Level 1
                  If you plonk one in the middle of a word, it might prevent that word from hyphenating. That would cause reflow.

                  In a case like this, you need to take a close look at a specific, if you can narrower it down. Compare the before and after composition and see where the first change occurs. Then look for index markers in the vicinity.