5 Replies Latest reply on Jan 14, 2009 9:25 AM by (Morgan_Routledge)

    Add is not a funtion (when adding a topic)

      app.documents[0].indexes[0].indexSections.item(2).topics.add(); returns with 'is not a function'. I'm not sure if this is a bug but for the life of me, I can't call that method. app.documents[0].indexes[0].indexSections.item(2).topics.count() is vaild and returns with an integer and app.documents[0].indexes[0].indexSections.item(2).topics.constructor.name returns 'topics' so I know i'm in the right context. Does anyone have any insight?

      To give you a little more information, I am trying to create an index from a word list but i'm running into problems creating a new topic entry.
        • 1. Re: Add is not a funtion (when adding a topic)
          Level 1
          I understand that topics.add() is called with arguments but it still halts on add is not a function. I can call other functions like count() and I've used add() for other objects. Has anyone ever tried to script the creation of a index?
          • 2. Re: Add is not a funtion (when adding a topic)
            Martin Fischer Level 2
            Hi Morgan,

            take a look at an example of Peter Kahrel in http://www.hilfdirselbst.ch/foren/Indizierung_automatisieren_P270961.html#270961
            or at another example (IndexFromList_CS3.jsx) in http://www.hilfdirselbst.ch/foren/Skript_zum_Index_generieren_P322824.html#322824
            or http://www.adobeforums.com/webx/.59b4db8c

            Martin Fischer
            • 3. Re: Add is not a funtion (when adding a topic)
              Level 1
              Thanks Martin, I'll give that a try. I've found it hard finding resources for scripting indesign, I appreciated the help. Is there a large Germain community for scripting indesign?
              • 4. Re: Add is not a funtion (when adding a topic)
                Level 1
                Hi Morgan,

                If you look at IndexSections in the Object Model Viewer in the ESTK, you'll see that it doesn't have an add method. That's because Index sections are added by InDesign based on the topics in the index. Why not just add a topic? Here's an example that shows how to add a topic and subtopics using the findText method:

                //An InDesign CS4 JavaScript
                //Shows how to create an index, create a topic, create subtopics, and add page references.
                function main(){
                function mySnippet(){
                var myFoundItem;
                //Reset find/change text preferences.
                app.findTextPreferences = NothingEnum.nothing;
                app.changeTextPreferences = NothingEnum.nothing;
                app.findTextPreferences.findWhat = "Widget ^?";
                //Make an example document.
                var myDocument = myCreateExampleDocument();
                var myIndex = myDocument.indexes.add(0);
                var myTopic = myIndex.topics.add("Widget");
                var mySubtopicA = myTopic.topics.add("A");
                var mySubtopicB = myTopic.topics.add("B");
                var mySubtopicC = myTopic.topics.add("C");
                var myFoundItems = myDocument.findText();
                for(var myCounter = myFoundItems.length-1; myCounter >= 0; myCounter --){
                  myFoundItem = myFoundItems[myCounter];
                   case "A":
                   case "B":
                   case "C":
                //Reset find preferences.
                app.findTextPreferences = NothingEnum.nothing;
                function mySetup(){
                var myString = "If you're looking for something to buy, you might consider our Widget A (Part Number: X1234-431, $24.95), "
                myString += "or Widget B (Part Number: X1-4, $74.95).\r";
                myString += "Other options include Widget C (Part Number: X123-43, $135.95) and ";
                myString += "Widget D (Part Number: X12345-4321, $5.95).\r";
                //Create an example document.
                var myDocument = app.documents.add();
                //Create a text frame on page 1.
                var myTextFrame = myDocument.pages.item(0).textFrames.add();
                //Set the bounds of the text frame.
                myTextFrame.geometricBounds = myGetBounds(myDocument, myDocument.pages.item(0));
                //Fill the text frame with example text.
                myTextFrame.contents = myString;
                function myTeardown(){
                function myGetBounds(myDocument, myPage){
                var myPageWidth = myDocument.documentPreferences.pageWidth;
                var myPageHeight = myDocument.documentPreferences.pageHeight
                if(myPage.side == PageSideOptions.leftHand){
                  var myX2 = myPage.marginPreferences.left;
                  var myX1 = myPage.marginPreferences.right;
                  var myX1 = myPage.marginPreferences.left;
                  var myX2 = myPage.marginPreferences.right;
                var myY1 = myPage.marginPreferences.top;
                var myX2 = myPageWidth - myX2;
                var myY2 = myPageHeight - myPage.marginPreferences.bottom;
                return [myY1, myX1, myY2, myX2];


                • 5. Re: Add is not a funtion (when adding a topic)
                  Level 1
                  I was able to get my script working. Thanks Ole and Marin.