8 Replies Latest reply on Dec 6, 2011 6:50 AM by KeithGilbert

    [JS CS5.5] Index topic sort order messes up script

    KeithGilbert Level 1

      I'm stumped on this one. If anyone has a suggestion, I'd appreciate it!

       

      The script below iterates through my index, and works just fine if none of the index entries have "Sort By" text that causes the entry to move DOWN in the list alphabetically. It works fine if the "Sort By" text causes the index entry to move UP in the list alphabetically.

       

      ===== CODE

       

      var myTopics = myDoc.indexes[0].allTopics;

                for (var i = myTopics.length-1; i > -1; i--) { // for each index topic...

                          for (var j = myTopics[i].pageReferences.length-1; j > -1; j--) { // for each page reference in the topic...

                                    var myAttributeValue = topic_path(myTopics[i], myTopics[i].name);

                                    var myInsertionPoint = myTopics[i].pageReferences[j].sourceText; // THIS LINE THROWS THE ERROR

                                    // Do something at the insertion point...

                          }

                }

      }

       

      // Create topic string. Subtopics are separated by '#'

      function topic_path (top, str) {

                if (top.parent.constructor.name == 'Index') {

                          return str;

                }

                else {

                          return topic_path (top.parent, top.parent.name + ', ' + str);

                }

      }

       

      ===== END CODE

       

      So it appears to me that since the script is iterating through the page references and the index entries from the bottom up, it gets to a topic that is displayed lower the in list, but it actually "belongs" up higher in the list alphabetically, then the script can't get match the page reference to the source text.

       

      Is there any way to interate through index entries other than what I'm doing? I can't think of any other workarounds.

        • 1. Re: [JS CS5.5] Index topic sort order messes up script
          Peter Kahrel Adobe Community Professional & MVP

          Keith,

           

          I don't see what you're trying to do. You mention that you change the sort order of the topics but it's not clear where you do that. And you create strings of a topic name followed by its subtopics names, but it's not clear what you do with those strings. If you want to change sort orders, you'd look for topics, not page references, so you don't need the pageReference for-loop. Could you clarify what you want a bit more?

           

          But if you do want to change the sort order, what you describe will always be a problem. Since InDesign immediately sorts the topic list whenever you change the Sort By field, the references to your array (myTopics) are likely to become invalid.

           

          Peter

          • 2. Re: [JS CS5.5] Index topic sort order messes up script
            KeithGilbert Level 1

            Sorry, my original post wasn't very clear. I'll try to explain more clearly:

             

            Here is the big picture: I'm scripting a solution that takes a completed index, harvests the index references, and inserts the references into document structure XML that we eventually output.

             

            So we have a completed index, with 2 or 3 levels of topics. Some of the topics have a "Sort by" entry in Page Reference Options, so that the entry sorts differently than it is spelled. See "and rituals" in the screen shot below. That entry would normally appear in the "a" position, but is sorted by "rituals" so it appears in the "r" position in the alphabetical list.

             

            screenshot1.jpg

            So, the script code that I posted in the original post iterates through each index topic from Z to A, and within each topic iterates through the page references from last to first. For each page reference, the script tries to resolve an insertion point for the page reference, and then goes off and adds code to the XML for the index entry.

             

            The script works perfect, except when it hits a page reference that is displayed lower in the list than it would if it didn't have a "Sort by" entry. In this case, the script is unable to get the insertion point for the page reference, for some reason.

            • 3. Re: [JS CS5.5] Index topic sort order messes up script
              [Jongware] Most Valuable Participant

              It's the adding XML code that messes your document insertion points up. I fell for that one too, these supposedly invisible codes take up *two* character positions.

               

              I could solve it for my case by careful counting :)

               

              Perhaps you could sort your topic references by their actual insertionpoint order before adding XML into your running text.

              • 4. Re: [JS CS5.5] Index topic sort order messes up script
                KeithGilbert Level 1

                Jongware, thanks for the help, but I don't think that is what is going on in this case. I've removed all the code that is adding XML elements, and I still get an "Object is invalid" error on the "var myInsertionPoint = myTopics[i].pageReferences[j].sourceText;" line in my original post.

                 

                In other words, in my original code, in the "Do something at the insertion point" line, I'm not currently doing ANYTHING. I'm just trying to get the insertion point. I'm not altering or manipulating the original text, the index, or the XML at all.

                • 5. Re: [JS CS5.5] Index topic sort order messes up script
                  Peter Kahrel Adobe Community Professional & MVP

                  Keith -- Apparently any values in the Sort By field interact in mysterious ways with the rest of the world. Maybe you could try something like this:

                  1. cycle through all topics and subtopics, adding the value of each (sub)topic's Sort By field as a script label to the (sub)topic -- then blank the Sort By field;

                  2. // Do something at the insertion point...

                  3. cycle through all topics again, restoring the Sort By field from what you stored in the topic's script label.

                   

                  This shouldn't be necessary, but it looks as if it is.

                   

                  Peter

                  • 6. Re: [JS CS5.5] Index topic sort order messes up script
                    KeithGilbert Level 1

                    Peter, I was thinking that I'd need to do some sort of workaround like this. But how can I add the value of each (sub)topic's Sort By field as a script label to the (sub)topic? It appears that page references can have labels, but not topics. Or am I missing something obvious here?

                    • 7. Re: [JS CS5.5] Index topic sort order messes up script
                      Peter Kahrel Adobe Community Professional & MVP

                      No, you're not missing anything! Sorry, I had just assumed that since you can add script labels to virtually anything, you could add them to topics as well. But now that I checked it turns out that you can't. Two ways out of this could be

                      (1) temporarily add the Sort By value to the topic's name separated by a unique string, blank the Sort By field. Later restore the Sort By field and the topic's name;

                      (2) if for some reason you can't add the Sort By value to the name, you'll have to build a table with topic names and Sort By values, blank the Sort By fields, then restore them later using the table.

                      Pretty convoluted, but I can't think of anything else.

                       

                      Peter

                      • 8. Re: [JS CS5.5] Index topic sort order messes up script
                        KeithGilbert Level 1

                        Thanks Peter. This was really helpful. I was able to append the Sort By value to the topic name separated by #####, delete the sort value, do what I needed to do, and then restore the sort value. Works great! Thanks for the help and advice!