2 Replies Latest reply on May 23, 2015 3:12 AM by Vamitul

    Sorting Document Width and Open Files

    BEGINNER_X Level 3

      Hi All,

       

      Final Request:

      My request is combine multiple files into one as per document width. (i.e., 20 files into 1)


      Please help how to open the width sorted files again:

       

      var  myFolder = Folder.selectDialog("Select the Folder contains Indesign Files");
      var myFiles = myFolder.getFiles("*.indd");
      myWidthArray = new Array();
      
      for(i=0; i<myFiles.length; i++)
      {
          app.open(myFiles[i]);
          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;  
          
          var myDocument_Width = app.activeDocument.documentPreferences.pageWidth;
          alert("Doc_Name: " + app.activeDocument.name + "\rWidth: " + myDocument_Width);
          
          myWidthArray.push(myDocument_Width)
          
          app.activeDocument.close();
          }
      
      alert(myWidthArray)
      var myPoints = myWidthArray;
      myPoints.sort(function(a, b){return a-b});
      alert("myPoints: " + myPoints)
      
      
      //here need help
      for(k=0; k<myPoints.length; k++)
      {
          app.open(myPoints[k])
          }
      

       

       

      Once I open the file again, then I integrate with the existing forum script

       

      var destination_doc = app.activeDocument;
      destination_doc.documentPreferences.allowPageShuffle = true;  
      destination_doc.spreads[-1].allowPageShuffle = true;  
      
      
      
      // select the source folder
      var sourceFolder = Folder.selectDialog("Select a folder with source InDesign files.");
      
      // if we're not in the source folder, stop running the script.
      if ( !sourceFolder ) {
          exit(0);
      }
      
      // set file list and run through each file. Sorting them alphanumerically.
      var fileList = sourceFolder.getFiles();
      fileList.sort();
      
      // run through each file
      for ( var i = 0; i < fileList.length; i++ ) {
          var source_file = fileList[i];
          // making sure it's an indesign file...
          if ( source_file instanceof File && source_file.name.match(/\.indd$/i)) {
              app.open(source_file);
              var source_doc = app.documents.item(source_file.name);
              var sourcePages = source_doc.pages.item(0);
              // break the master page items so they can be moved onto the new document.
              var masterItems = sourcePages.masterPageItems;
              if ( masterItems.length > 0 ) {
                  for ( var j=0; j<masterItems.length; j++ ) {
                      masterItems[j].override(sourcePages);
                  }
              }
              // removing the applied master (this can mess up some files if not done)
              sourcePages.appliedMaster=null;
              // duplicating it in the original file (due to errors) and them moving it to
              // the destination document.
              sourcePages.duplicate(LocationOptions.AFTER, source_doc.pages.item(0));
              sourcePages.move(LocationOptions.AFTER, destination_doc.pages.item(-1));
              // closes the file that was opened without saving (to avoid memory problems)
              app.activeDocument.close(SaveOptions.NO);
          }
      }
      

       

      Appreciate, any other better way.

       

      Thanks in advance

      Siraj

        • 1. Re: Sorting Document Width and Open Files
          BEGINNER_X Level 3

          Hi All,

          Request for first level of script...

           

          Need to open Indesign Files as per Trim Width.

           

          This is my script(using associative Array)

           

          var  myFolder = Folder.selectDialog("Select the Folder contains Indesign Files");  
          var myDocs = myFolder.getFiles("*.indd");  
          testArray={};
          
          app.scriptPreferences.userInteractionLevel=UserInteractionLevels.NEVER_INTERACT;
          
          for (var i=0;i<myDocs.length;i++)
          {
            app.open(myDocs[i]);
            var docName = myDocs[i].name;
            var docWidth = app.activeDocument.documentPreferences.pageWidth;
            testArray[docName] = docWidth;
            app.activeDocument.close();
          }
          
          for (var i in testArray)
          {
             alert('testArray[\''+ i +'\'] is ' + testArray[i]);
          }
          
          //To sort the associative array by page wo
          testArray.sort(function(a,b) {
              return a.docWidth - b.docWidth;
          });
          
          //Next you can open the sorted array (i.e open the sorted documents by page width)
          
          for (var i in testArray)
          {
             app.open(i);
             //do something as per yourrequirement
          }
          

           

           

           

           

          Error Screenshot is below:

           

          Screen Shot 2015-05-19 at 8.11.55 PM.png

           

          Thanks in Advance

          Siraj

          • 2. Re: Sorting Document Width and Open Files
            Vamitul Level 4

            Hi Siraj,

            You cannot use .sort() with an associative array.
            Either use a plain array and sort function, or use the associative array and sort it "by hand".

            Looking at your code, i don't see any reason to use associative.