1 Reply Latest reply on May 10, 2010 6:14 AM by Ajv55

    Removing temporary documents

    Ajv55 Level 1

      I'm working on a script that exports all the groups of a document to individual PDF files. It's working smoothly on lightweight documents (just a couple of pages with some really basic elements), but when I try it on a "real" document, it crashes InDesign (CS3). The crash always happens after the first group has been exported and it starts to export the next.

       

      When I look in the export folder, there's always 1 pdf and 2 lock files (.idlk). Also, every time I reopen InDesign, the temporary document I use in exporting is still in memory, it's in the window like any other recovered document.

       

      So I'm guessing that it crashes because it runs out of memory when there are several temporary docs open at the same time. Or something like that... Is there a way to force-remove the temp doc, or some other way to do garbage collection/cleaning memory?

       

      Here's the code

       

       

      /***********************
           
           Export all Groups of the document to individual PDF files.     
           
      ***/
      
      //Some vars, includes old stuff too...
      var page;
      var pageIndex = 0;
      var pages = app.activeDocument.pages.length;
      var group;
      var groupCounter = 0;
      var groupIndex = 0;
      var groups;
      var groupBounds;
      var groupWidth;
      var groupHeight;
      var date = new Date();
      var dateString = pad(date.getHours(), 2)  +  pad(date.getMinutes(), 2) + pad(date.getSeconds(), 2);
      var newDoc = app.documents.add(false);
      
      //Add listener to the newDoc document
      newDoc.eventListeners.add("afterExport", processNextGroup);         
      
      processPage(pageIndex);
      
      function processPage(index){
      
           //If we try to handle a page that doesn't exist
           if (index >= pages) return;
      
           //get all the groups of the page
           groups = app.activeDocument.pages.item(index).groups;
      
           //If there aren't any groups on this page, go to the next
           if (groups.length == 0){
                     processPage(index + 1);               
                     return;
           }
      
           //Update the global var
           pageIndex = index;
           
           //Start processing the first group on the page
           processGroup(groupIndex);
      
      }
      
      function processGroup(groupIndex){
           
                //Get the group and its boundaries
                group = groups.item(groupIndex);
                groupBounds = group.geometricBounds;
                
                //Calculate group's height & width
                groupHeight = groupBounds[2] - groupBounds[0] + 10;
                groupWidth = groupBounds[3] - groupBounds[1] + 10;
                
                //OLD, we only use one document now! Create a new document but don't show it on screen
                //var newDoc = app.documents.add(false);
                
                //Set preferences of the newDoc
                with (newDoc.documentPreferences) {
                     pageHeight = groupHeight + "mm";
                     pageWidth = groupWidth + "mm";
                     pagesPerDocument = 1;
                }
                
                //Create a duplicate of the group and place it in the newDoc
                var d = group.duplicate(newDoc.pages.item(0));
                
                //Move it to the top left corner (5 mm margins)
                d.move([5,5]);
                
                //Start exporting
                
                //OLD    
                //newDoc.eventListeners.add("afterClose", getRidOfDoc);     //SOMEHOW DOESN'T WORK??
                
                //Create a filename
                var fileName = "C:\\PDFS\\page_"+ (pageIndex+1) + "_group_" + (groupIndex + 1) + "_" + dateString + ".pdf";
      
                //Export  the file
                newDoc.exportFile(ExportFormat.pdfType, (new File(fileName)), false, app.pdfExportPresets[0], "", true);
                
                //Remove the group from the page
                d.remove();
                
                //processNextGroup(null);
                
                // (OLD) Close the file (would be smarter to close this in processNextGroup, after the afterExport, but that doesn't work in ID.
                //newDoc.close();          
                //newDoc.remove();
                
                // ---> When the exporting is done, the eventlistener runs the processNextGroup function.
           
      }
      
      function getRidOfDoc(event){
           
           alert("destroyed, not");     
           
           //Removes the eventlisteners of the 
           event.currentTarget.removeEventListener("afterExport", processNextGroup);     
           event.currentTarget.removeEventListener("afterClose", getRidOfDoc);     
      
      }
      
      function processNextGroup(event){
      
           alert("Starting next");
           
           //If we have reached the last group on the page
           if (groupIndex == groups.length - 1){
      
                     //Check if we have processed the last page of the document
                     if ((pageIndex + 1) == pages){
                          alert("END");
                     } else {
                          //If there are pages to be processed, update the counters and process the next page
                          pageIndex += 1;
                          groupIndex = 0;
                          processPage(pageIndex);     
                     }
                     
                } else {
                     //If we haven't processed the last group on the page, update the counter and process the next group
                     groupIndex = groupIndex + 1;
                     processGroup(groupIndex);
                
                }
           
      }
      
      function pad(number, length) {
      
          var str = '' + number;
          while (str.length < length) {
              str = '0' + str;
          }
      
          return str;
      
      }
      
        • 1. Re: Removing temporary documents
          Ajv55 Level 1

          Update: I modified the script, now it first creates a bunch of documents that have one group each, and then it tries to export all those files as pdf. Same error occurs after the first pdf has been created, ID crashes without warning.

           

          Here's the updated code:

           

           

          /***********************
               
               Export all Groups of the document to individual PDF files.     
               v 0.2
               
          ***/
          
          //Some vars, includes old stuff too...
          var page;
          var pageIndex = 0;
          var pages = app.activeDocument.pages.length;
          var group;
          var groupCounter = 0;
          var groupIndex = 0;
          var groups;
          var groupBounds;
          var groupWidth;
          var groupHeight;
          var date = new Date();
          var dateString = pad(date.getHours(), 2)  +  pad(date.getMinutes(), 2) + pad(date.getSeconds(), 2);
          //var newDoc = app.documents.add(false);
          
          var newDocs = [];
          var fileNames = [];
          
          var mainDoc = app.activeDocument;
          
          //Add listener to the newDoc document
          //newDoc.eventListeners.add("afterExport", processNextGroup);     
          
          processPage(pageIndex);
          
          function processPage(index){
          
               //If we try to handle a page that doesn't exist
               if (index >= pages) {
                    end();
                    return;
               }
          
               //get all the groups of the page
               groups = mainDoc.pages.item(index).groups;
          
               //If there aren't any groups on this page, go to the next
               if (groups.length == 0){
                         processPage(index + 1);               
                         return;
               }
          
               //Update the global var
               pageIndex = index;
               
               //Start processing the first group on the page
               processGroup(groupIndex);
          
          }
          
          function processGroup(groupIndex){
               
                    //Get the group and its boundaries
                    group = groups.item(groupIndex);
                    groupBounds = group.geometricBounds;
                    
                    //Calculate group's height & width
                    groupHeight = groupBounds[2] - groupBounds[0] + 10;
                    groupWidth = groupBounds[3] - groupBounds[1] + 10;
                    
                   
                    var newDoc = app.documents.add(false);
                    newDocs.push(newDoc);
                    
                    //Set preferences of the newDoc
                    with (newDoc.documentPreferences) {
                         pageHeight = groupHeight + "mm";
                         pageWidth = groupWidth + "mm";
                         pagesPerDocument = 1;
                    }
                    
                    //Create a duplicate of the group and place it in the newDoc
                    var d = group.duplicate(newDoc.pages.item(0));
                    
                    //Move it to the top left corner (5 mm margins)
                    d.move([5,5]);
                    
                    //Start exporting
                    
                    
                    //newDoc.eventListeners.add("afterClose", getRidOfDoc);     //SOMEHOW DOESN'T WORK??
                    
                    //Create a filename
                    var fileName = "C:\\PDFS\\page_"+ (pageIndex+1) + "_group_" + (groupIndex + 1) + "_" + dateString + ".pdf";
                    fileNames.push(fileName);
               
               
                    //Export  the file
                    //newDoc.exportFile(ExportFormat.pdfType, (new File(fileName)), false, app.pdfExportPresets[0], "", true);
                    
                    //Remove the group from the page
                    //d.remove();
                    
                    processNextGroup(null);
                    
                    // (OLD) Close the file (would be smarter to close this in processNextGroup, after the afterExport, but that doesn't work in ID.
                    //newDoc.close();          
                    //newDoc.remove();
                    
                    // ---> When the exporting is done, the eventlistener runs the processNextGroup function.
               
          }
          
          function getRidOfDoc(event){
               
               alert("destroyed, not");     
               
               //Removes the eventlisteners of the 
               event.currentTarget.removeEventListener("afterExport", processNextGroup);     
               event.currentTarget.removeEventListener("afterClose", getRidOfDoc);     
          
          }
          
          function processNextGroup(event){
          
               //alert("Starting next");
               
               //If we have reached the last group on the page
               if (groupIndex == groups.length - 1){
          
                         //Check if we have processed the last page of the document
                         if ((pageIndex + 1) == pages){
                              end();
                         } else {
                              //If there are pages to be processed, update the counters and process the next page
                              pageIndex += 1;
                              groupIndex = 0;
                              processPage(pageIndex);     
                         }
                         
                    } else {
                         //If we haven't processed the last group on the page, update the counter and process the next group
                         groupIndex = groupIndex + 1;
                         processGroup(groupIndex);
                    
                    }
               
          }
          
          function end(){
               alert("end");
               
               for (var i = 0; i < newDocs.length; i++){
                    
                    newDocs[i].exportFile(ExportFormat.pdfType, (new File(fileNames[i])), false, app.pdfExportPresets[0]);
                         alert("end2");
               }
               
          }
          
          function pad(number, length) {
          
              var str = '' + number;
              while (str.length < length) {
                  str = '0' + str;
              }
          
              return str;
          
          }
          

           

           

          Message was edited by: Ajv55