4 Replies Latest reply on Jun 26, 2012 11:17 AM by absqua

    Why won't this script work?

    KuddRoww Level 1

      Alright so yesterday I posted on an issue I had with joining InDesign files, I thought I'd found a solution and I was wrong. Nothing quite so humbling as scripting... Anyways the gist of what I'm trying to do is I'm trying to combine a whole bunch of InDesign files into one big document. The issue I'm encountering is that InDesign crashes. The thing that is strange to me is that a single iteration of this script works fantastically, however when I try to do it with multiple files InDesign ***** the bed.

       

      Any and all help in figure this out would be extremely appreciated.

       

      System: WIndows 7

      Indy Version: CS5

       

       

       

      1) Select folder.

      2) Get files.

      3) Open one at a time and duplicate the pages into another document. (Which I'm calling TOC because I basically that's what I want to make with the giant document.)

      4) Close the source file without saving. The pages shouldn't change but better safe than sorry.

      5) Close the destination file (TOC) and save it with the new pages on it.

       

      #target InDesign

       

      folder = Folder.selectDialog();

      files = folder.getFiles("*.INDD");

      for(var i = 0; i<files.length; i++){

          source_doc = app.open(files[i]);

          destination_doc = app.open(File("c://users//user//desktop//TOC.indd"))

          rest();

      }

       

      function rest(){

      sourcePages = source_doc.pages.itemByRange(0,-1);

      sourcePages.duplicate(LocationOptions.AFTER, destination_doc.pages.item(-1));

      source_doc.close(SaveOptions.NO);

      destination_doc.close(SaveOptions.YES, "c://users//user//desktop//TOC.indd");

      }

        • 1. Re: Why won't this script work?
          absqua Level 4

          Couple thoughts: The code is kind of messy. I don't understand why you're opening and closing your destination doc on each iteration, or why you have some stuff split out into another function, or why you don't have any vars. As a general rule, I try to work on spreads rather than pages when possible. I also avoid itemByRange() because of the weird plural object it returns. Here's a (to my mind, anyway) cleaned-up script:

           

          var folder = Folder.selectDialog(),
              files = folder.getFiles("*.INDD"),
              destination_doc = app.open(new File("c://users//user//desktop//TOC.indd")),
              source_doc, i, j;
          
          for (i = 0; i < files.length; i++) {
              source_doc = app.open(files[i]);    
              for (j = 0; j < source_doc.spreads.length; j++) {
                  source_doc.spreads[j].duplicate(LocationOptions.AT_END, destination_doc);
              }
              source_doc.close(SaveOptions.NO);
          }
          
          destination_doc.close(SaveOptions.YES);
          

           

          See if you have better luck with it.

           

          Jeff

          • 2. Re: Why won't this script work?
            KuddRoww Level 1

            Hi Jeff,

             

            Ran it through and same issue. A single file works brilliantly, however when I try an do it with two files InDesign crashes.

             

            The part that is pehaps key at this point is the fact that I can run the script once on a single file and it works, the file is duplicated. If I run the script again on the same file it crashes. It seems like it doesn't want to add to the end of the document if there are already contents in it. Any thoughts? I'm stumped.

             

            Thank-you again for your prompt and helpful feedback!

            • 3. Re: Why won't this script work?
              KuddRoww Level 1

              I tried it again. It is the files. When I made single page documents of one textframe it worked fine. Now the uneviable task of figuring out what is wrong with these legacy files that they won't hop over.

               

              Thank-you again!

              • 4. Re: Why won't this script work?
                absqua Level 4

                You could put in a step that exports each converted source doc to idml, and then open that. Might help?

                 

                var folder = Folder.selectDialog(),
                    files = folder.getFiles("*.INDD"),
                    destination_doc = app.open(new File("c://users//user//desktop//TOC.indd")),
                    source_doc, idmlFile, i, j;
                
                for (i = 0; i < files.length; i++) {
                    source_doc = app.open(files[i]);
                    if (!source_doc.saved) {
                        idmlFile = new File(Folder.temp + "/" + files[i].displayName.replace(".indd", ".idml"));
                        source_doc.exportFile(ExportFormat.INDESIGN_MARKUP, idmlFile);
                        source_doc.close(SaveOptions.NO);
                        source_doc = app.open(idmlFile);
                    }
                    for (j = 0; j < source_doc.spreads.length; j++) {
                        source_doc.spreads[j].duplicate(LocationOptions.AT_END, destination_doc);
                    }
                    source_doc.close(SaveOptions.NO);
                }
                
                destination_doc.close(SaveOptions.YES);