1 Reply Latest reply on Jul 20, 2012 5:46 PM by biomikez

    Creating a TOC via script on InDesign Server [CS5.5]

    BrettFattori Level 1

      I have been struggling for about two weeks with trying to figure out how to script the creation of a TOC and an index for a book.  I've successfully created the book, and have also been able to create the table of contents, however I cannot place the contents in the book.  From the UI it's simple because it loads up the cursor with the TOC contents.  But in script, it simply provides an array with a single Story object.

       

      I have no idea how to make the story appear in my document.  Here's my current code:

       

      var book = app.open("book.indb");

      var doc = app.open("table_of_contents.indd");

      var tocStyle = doc.tocStyles.firstItem();

      var stories = doc.createTOC(tocStyle, true, book, ["1in", "1in"], false, doc.layers.firstItem());

       

      When I hit that final line, I get the following error:

      Invalid value for parameter 'destinationLayer' of method 'createTOC'. Expected Layer, but received Layer.

       

      Okay... so, if it's expecting a Layer and I'm providing a Layer - what gives?

       

      Originally I had figured that I:

      1. Had to create a TOC document from a template which contains a single text frame that automatically flows from page-to-page (using a master)
      2. Add the document to the book at the beginning
      3. Copy the contents of the Story into the TextFrame
      4. Iterate the Lines collection of the Story, copying the paragraph and character styles for each line

       

      This works, but it's no longer a TOCStory type.  I would like to maintain the TOCStory type and just have the content flowed into the new document which I add.  This makes sense, logically, but I know that nothing in InDesign is really that logical.  It seems to be a lot of collection manipulations, but I cannot figure out which collections to manipulate.

       

      The Index.generate() method seems so much more logical because the first argument is where to place the content.  If it were only that simple for the TOC!  If you've had experience doing this, please let me in on the secret...  I don't have access to the menu items so I cannot invoke actions.  Those threads don't seem to apply for InDesign Server (and that makes sense, since it's headless)

       

      Thanks,

      - Brett

        • 1. Re: Creating a TOC via script on InDesign Server [CS5.5]
          biomikez Level 1

          Hi Brett,

           

          here is the code that worked for me.

           

          Michael

           

           

          var bk=app.books.add(File("C:/Users/wwadmin/Desktop/testbook.indb"));
          for(i=0; i<5; i++){
          var doc=app.documents.add()
          var page=doc.pages.firstItem()
          var frame=page.textFrames.add({geometricBounds:[0,0,'10in','10in']})
          frame.contents="Hello World!!!!!";
          var style=doc.paragraphStyles.add({name:"Body"});
          frame.paragraphs.firstItem().appliedParagraphStyle=style;
          doc.save(new File("C:/Users/wwadmin/Desktop/doc"+i+".indd"))
          bk.bookContents.add(File("C:/Users/wwadmin/Desktop/doc"+i+".indd"));
          }

          var doc=app.documents.add();
          var page=doc.pages.firstItem();
          style=doc.paragraphStyles.add({name:"Body"})
          tocstyle=doc.tocStyles.add({name:"myStyle"});
          tocstyle.tocStyleEntries.add({formatStyle:style, name:style.name});
          doc.save(new File("C:/Users/wwadmin/Desktop/test.indd"))

          bk.bookContents.add(File("C:/Users/wwadmin/Desktop/test.indd"));

          doc.createTOC(tocstyle, true, bk, undefined, true, doc.layers.firstItem())
          bk.save()

          for(a=app.documents.length-1; a>(-1); a--){
          app.documents[a].save(File(app.documents[a].fullName));
          app.documents[a].close();
          }

          bk.close()