14 Replies Latest reply on Apr 27, 2016 12:36 AM by Johnwhite

    How to get subfolder indesign files

    Johnwhite Level 1

      Hi experts

       

      How can I tell the script what I want to get is the subfolders but not sub sub folder or sub sub sub...folder files.

       

      my script like this:

       

      function GetFiles(theFolder) {
      var files = [],
      fileList = theFolder.getFiles(),
      i, file;

      for (i = 0; i < fileList.length; i++) {
        file = fileList[i];
        if (file instanceof Folder) {
         files = files.concat(GetFiles(file));
        }
        else if (file instanceof File && file.name.match(/\.indd$/i)) {
         files.push(file);
        }
      }

      return files;
      }

       

      how can change it?

       

      thanks

      Regard

      John

        • 1. Re: How to get subfolder indesign files
          natrev Level 3

          Hi John,

           

          Try this code..

           

          var files = [],file,flag=0;
          var theFolder = Folder.selectDialog ("select folder");
          alert((GetFiles(theFolder)).join("\n"));
          
          function GetFiles(theFolder) {
              fileList = theFolder.getFiles();
              for (i = 0; i < fileList.length; i++) {
                file = fileList[i];
                if (file instanceof Folder) {
                    if(flag==0){
                      flag=1;
                      GetFiles(file);
                      }
                  }
                else if (file instanceof File && file.name.match(/\.indd$/i)) {
                 files.push(file);
                }
              }
              return files;
              }
          

           

          -yajiv

          1 person found this helpful
          • 2. Re: How to get subfolder indesign files
            Johnwhite Level 1

            Thank you yajiv

            thank you so much,

             

            but not work.

             

            regard

            John

            • 3. Re: How to get subfolder indesign files
              natrev Level 3

              Hi John,

               

              I have tested and Its working fine. What type error you have faced and please share any error screen shot to proceed further.

               

              -yajiv

              • 4. Re: How to get subfolder indesign files
                Johnwhite Level 1

                Hi Yajiv

                 

                please test whole of this script:

                 

                //======================================================================================
                var gScriptName = "Batch export INDD-files to PDF"; // Name of the script
                var gScriptVersion = "1.0"; // Version

                var gSet = GetSettings();
                CreateDialog();

                //===================================== FUNCTIONS  ======================================
                function CreateDialog() {
                var pdfPresetsList =app.pdfExportPresets.everyItem().name;
                var win = new Window("dialog", gScriptName + " - " + gScriptVersion);

                win.p = win.add("panel", undefined, "");
                win.p.alignChildren = "right";
                win.p.alignment = "fill";

                win.p.g = win.p.add("group");
                win.p.g.st = win.p.g.add("statictext", undefined, "PDF-presets:");
                win.p.g.ddl = win.p.g.add("dropdownlist", undefined, pdfPresetsList);
                win.p.g.ddl.selection = gSet.selectedPdfPresetIndex;
                win.p.g.ddl.preferredSize.width = 220;

                win.buttons = win.add("group");
                win.buttons.orientation = "row";  
                win.buttons.alignment = "center";
                win.buttons.ok = win.buttons.add("button", undefined, "OK", {name:"ok" });
                win.buttons.cancel = win.buttons.add("button", undefined, "Cancel", {name:"cancel"});

                var showDialog = win.show();

                if (showDialog == 1) {
                  gSet.selectedPdfPresetIndex = win.p.g.ddl.selection.index;
                  app.insertLabel("Kas_" + gScriptName + gScriptVersion, gSet.toSource());
                  Main();
                }
                }
                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                function Main() {
                var folder = Folder.selectDialog("Select a folder with InDesign files to export");
                if (folder == null) exit();
                var files = GetFiles(folder);
                var pdfFolder = new Folder(folder.fsName + "/" + "Pdf");
                VerifyFolder(pdfFolder);

                if (files.length == 0) {
                  alert("No files to open.", gScriptName + " - " + gScriptVersion);
                  exit();
                }

                var pdfPresets =app.pdfExportPresets.everyItem().getElements();
                var selectedPdfPreset = pdfPresets[gSet.selectedPdfPresetIndex];
                var count = 1;
                app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

                // Progress bar -----------------------------------------------------------------------------------
                var progressWin = new Window ("window", gScriptName + " - " + gScriptVersion);
                var progressBar = progressWin.add ("progressbar", [12, 12, 350, 24], 0, files.length);
                var progressTxt = progressWin.add("statictext", undefined, "Starting exporting files");
                progressTxt.bounds = [0, 0, 340, 20];
                progressTxt.alignment = "left";
                progressWin.show();
                // Progress bar -----------------------------------------------------------------------------------

                for (var i = 0; i < files.length; i++) {
                  var currentFile = files[i];
                 
                  try {
                   var doc = app.open(currentFile, false);
                   var docName = doc.name;

                   // Progress bar -----------------------------------------------------------------------------------
                   progressBar.value = count;
                   progressTxt.text = String("Exporting file - " + docName + " (" + count + " of " + files.length + ")");
                   // Progress bar -----------------------------------------------------------------------------------

                   var file = new File(pdfFolder + "/" + GetFileName(docName) + ".pdf");
                   if (file.exists) {
                    var increment = 1;
                    while (file.exists) {
                     file = new File(pdfFolder + "/" + GetFileName(docName) + "(" + increment++ + ").pdf");
                    }
                   }
                 
                   doc.exportFile(ExportFormat.pdfType, file, false, selectedPdfPreset);
                   doc.close(SaveOptions.NO);
                   count++;
                  }
                  catch(e) {}
                }

                // Progress bar -----------------------------------------------------------------------------------
                progressWin.close();
                // Progress bar -----------------------------------------------------------------------------------

                alert("Done.", gScriptName + " - " + gScriptVersion);
                }
                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                function VerifyFolder(folder) {
                if (!folder.exists) {
                  var folder = new Folder(folder.absoluteURI);
                  var array1 = new Array();
                  while (!folder.exists) {
                   array1.push(folder);
                   folder = new Folder(folder.path);
                  }
                  var array2 = new Array();
                  while (array1.length > 0) {
                   folder = array1.pop();
                   if (folder.create()) {
                    array2.push(folder);
                   } else {
                    while (array2.length > 0) {
                     array2.pop.remove();
                    }
                    throw "Folder creation failed";
                   }
                  }
                }
                }
                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                function GetFiles(theFolder) { 
                    var files = [],file,flag=0; 
                        fileList = theFolder.getFiles(); 
                    for (i = 0; i < fileList.length; i++) { 
                        file = fileList[i]; 
                      if (file instanceof Folder) { 
                          if(flag==0){ 
                            flag=1; 
                            GetFiles(file); 
                            } 
                        } 
                      else if (file instanceof File && file.name.match(/\.indd$/i)) { 
                       files.push(file); 
                      } 
                    } 
                    return files; 
                    } 

                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                function GetFileName(fileName) {
                var string = "";
                var result = fileName.lastIndexOf(".");
                if (result == -1) {
                  string = fileName;
                }
                else {
                  string = fileName.substr(0, result);
                }
                return string;
                }
                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                function GetSettings() {
                var set = eval(app.extractLabel("Kas_" + gScriptName + gScriptVersion));
                if (set == undefined) {
                  set = { selectedPdfPresetIndex : 0 };
                }

                return set;
                }
                //---------------------------------------------------------------------------------------- ----------------------------------------------------------------

                 

                My goal is only gen the target folder and subfolder's indesign files to pdf,

                 

                but it seems not really work.

                 

                thanks

                Regard

                John

                • 5. Re: How to get subfolder indesign files
                  natrev Level 3

                  Hi John,

                   

                  Try this code...

                   

                  //======================================================================================
                  var gScriptName = "Batch export INDD-files to PDF"; // Name of the script
                  var gScriptVersion = "1.0"; // Version
                  var gSet = GetSettings();
                  var flag=0,files = [];
                  CreateDialog();
                  //===================================== FUNCTIONS  ======================================
                  function CreateDialog() {
                  var pdfPresetsList =app.pdfExportPresets.everyItem().name; 
                  var win = new Window("dialog", gScriptName + " - " + gScriptVersion);
                  win.p = win.add("panel", undefined, "");
                  win.p.alignChildren = "right";
                  win.p.alignment = "fill";
                  
                  
                  win.p.g = win.p.add("group");
                  win.p.g.st = win.p.g.add("statictext", undefined, "PDF-presets:");
                  win.p.g.ddl = win.p.g.add("dropdownlist", undefined, pdfPresetsList);
                  win.p.g.ddl.selection = gSet.selectedPdfPresetIndex;
                  win.p.g.ddl.preferredSize.width = 220;
                  win.buttons = win.add("group");
                  win.buttons.orientation = "row";   
                  win.buttons.alignment = "center";
                  win.buttons.ok = win.buttons.add("button", undefined, "OK", {name:"ok" });
                  win.buttons.cancel = win.buttons.add("button", undefined, "Cancel", {name:"cancel"});
                  var showDialog = win.show();
                  if (showDialog == 1) {
                    gSet.selectedPdfPresetIndex = win.p.g.ddl.selection.index;
                    app.insertLabel("Kas_" + gScriptName + gScriptVersion, gSet.toSource());
                    Main();
                  } 
                  }
                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                  function Main() {
                  var folder = Folder.selectDialog("Select a folder with InDesign files to export");
                  if (folder == null) exit();
                  var files = GetFiles(folder);
                  var pdfFolder = new Folder(folder.fsName + "/" + "Pdf");
                  VerifyFolder(pdfFolder);
                  if (files.length == 0) {
                    alert("No files to open.", gScriptName + " - " + gScriptVersion);
                    exit();
                  }
                  var pdfPresets =app.pdfExportPresets.everyItem().getElements();
                  var selectedPdfPreset = pdfPresets[gSet.selectedPdfPresetIndex];
                  var count = 1;
                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                  // Progress bar -----------------------------------------------------------------------------------
                  var progressWin = new Window ("window", gScriptName + " - " + gScriptVersion);
                  var progressBar = progressWin.add ("progressbar", [12, 12, 350, 24], 0, files.length);
                  var progressTxt = progressWin.add("statictext", undefined, "Starting exporting files");
                  progressTxt.bounds = [0, 0, 340, 20];
                  progressTxt.alignment = "left";
                  progressWin.show();
                  // Progress bar -----------------------------------------------------------------------------------
                  for (var i = 0; i < files.length; i++) {
                    var currentFile = files[i];
                    
                    try {
                     var doc = app.open(currentFile, false);
                     var docName = doc.name;
                     // Progress bar -----------------------------------------------------------------------------------
                     progressBar.value = count;
                     progressTxt.text = String("Exporting file - " + docName + " (" + count + " of " + files.length + ")");
                     // Progress bar -----------------------------------------------------------------------------------
                     var file = new File(pdfFolder + "/" + GetFileName(docName) + ".pdf");
                     if (file.exists) {
                      var increment = 1;
                      while (file.exists) {
                       file = new File(pdfFolder + "/" + GetFileName(docName) + "(" + increment++ + ").pdf");
                      }
                     }
                    
                     doc.exportFile(ExportFormat.pdfType, file, false, selectedPdfPreset);
                     doc.close(SaveOptions.NO);
                     count++;
                    }
                    catch(e) {}
                  }
                  // Progress bar -----------------------------------------------------------------------------------
                  progressWin.close();
                  // Progress bar -----------------------------------------------------------------------------------
                  alert("Done.", gScriptName + " - " + gScriptVersion);
                  }
                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                  function VerifyFolder(folder) {
                  if (!folder.exists) {
                    var folder = new Folder(folder.absoluteURI);
                    var array1 = new Array();
                    while (!folder.exists) {
                     array1.push(folder);
                     folder = new Folder(folder.path);
                    }
                    var array2 = new Array();
                    while (array1.length > 0) {
                     folder = array1.pop();
                     if (folder.create()) {
                      array2.push(folder);
                     } else {
                      while (array2.length > 0) {
                       array2.pop.remove();
                      }
                      throw "Folder creation failed";
                     } 
                    }
                  }
                  }
                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                  function GetFiles(theFolder) {  
                      var file;  
                          fileList = theFolder.getFiles();  
                      for (i = 0; i < fileList.length; i++) {  
                          file = fileList[i]; 
                          if(file.name==".DS_Store") continue;
                        if (file instanceof Folder) {  
                            if(flag==0){  
                              flag=1;  
                              GetFiles(file);  
                              }  
                          }  
                        else if (file instanceof File && file.name.match(/\.indd$/i)) {  
                         files.push(file);  
                        }  
                      }  
                      return files;  
                      } 
                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                  function GetFileName(fileName) {
                  var string = "";
                  var result = fileName.lastIndexOf(".");
                  if (result == -1) {
                    string = fileName;
                  }
                  else {
                    string = fileName.substr(0, result);
                  }
                  return string;
                  }
                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                  function GetSettings() {
                  var set = eval(app.extractLabel("Kas_" + gScriptName + gScriptVersion));
                  if (set == undefined) {
                    set = { selectedPdfPresetIndex : 0 };
                  }
                  return set;
                  }
                  

                   

                  - yajiv

                  • 6. Re: How to get subfolder indesign files
                    Kai Rübsamen Adobe Community Professional

                    Hi natrev,

                     

                    your script is not working correct, if there is already a subfolder 'PDF' at the first position in the folder. In this case, your script tries to find some 'indds' in the PDF-folder and does not see the real InDesign-files in the folder above!

                     

                    Kai

                    • 7. Re: How to get subfolder indesign files
                      Johnwhite Level 1

                      Thank you Kai

                       

                      Yes, your right,

                      So, I should move my PDF folder to desktop,

                      will then the script working correctly?

                       

                      Regard

                      John

                      • 8. Re: How to get subfolder indesign files
                        Kai Rübsamen Adobe Community Professional

                        No, I think the function which called the subfolder should be modified!

                        • 9. Re: How to get subfolder indesign files
                          Johnwhite Level 1

                          Hi Yajiv

                           

                          the script not working correctly.

                           

                          my goal is the indesign files which be contained in target folder and the subfolders

                          not just first subfolder.

                           

                          asume the target folder has 20 subfolder, and each folder contained some indesign files,

                          what I need is to gen those pdf.

                           

                          regard

                          John

                           

                          after change pdf folder path, this script like this:

                           

                          //====================================================================================== 

                          var gScriptName = "Batch export INDD-files to PDF"; // Name of the script 

                          var gScriptVersion = "1.0"; // Version 

                          var gSet = GetSettings(); 

                          var flag=0,files = []; 

                          CreateDialog(); 

                          //===================================== FUNCTIONS  ====================================== 

                          function CreateDialog() { 

                          var pdfPresetsList =app.pdfExportPresets.everyItem().name;  

                          var win = new Window("dialog", gScriptName + " - " + gScriptVersion); 

                          win.p = win.add("panel", undefined, ""); 

                          win.p.alignChildren = "right"; 

                          win.p.alignment = "fill"; 

                           

                           

                          win.p.g = win.p.add("group"); 

                          win.p.g.st = win.p.g.add("statictext", undefined, "PDF-presets:"); 

                          win.p.g.ddl = win.p.g.add("dropdownlist", undefined, pdfPresetsList); 

                          win.p.g.ddl.selection = gSet.selectedPdfPresetIndex; 

                          win.p.g.ddl.preferredSize.width = 220; 

                          win.buttons = win.add("group"); 

                          win.buttons.orientation = "row";    

                          win.buttons.alignment = "center"; 

                          win.buttons.ok = win.buttons.add("button", undefined, "OK", {name:"ok" }); 

                          win.buttons.cancel = win.buttons.add("button", undefined, "Cancel", {name:"cancel"}); 

                          var showDialog = win.show(); 

                          if (showDialog == 1) { 

                            gSet.selectedPdfPresetIndex = win.p.g.ddl.selection.index; 

                            app.insertLabel("Kas_" + gScriptName + gScriptVersion, gSet.toSource()); 

                            Main(); 

                          }  

                          //---------------------------------------------------------------------------------------- ---------------------------------------------------------------- 

                          function Main() { 

                          var folder = Folder.selectDialog("Select a folder with InDesign files to export"); 

                          if (folder == null) exit(); 

                          var files = GetFiles(folder); 

                          var pdfFolder = "~/Desktop/PDF_2/"; 

                          VerifyFolder(pdfFolder); 

                          if (files.length == 0) { 

                            alert("No files to open.", gScriptName + " - " + gScriptVersion); 

                            exit(); 

                          var pdfPresets =app.pdfExportPresets.everyItem().getElements(); 

                          var selectedPdfPreset = pdfPresets[gSet.selectedPdfPresetIndex]; 

                          var count = 1; 

                          app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL; 

                          // Progress bar ----------------------------------------------------------------------------------- 

                          var progressWin = new Window ("window", gScriptName + " - " + gScriptVersion); 

                          var progressBar = progressWin.add ("progressbar", [12, 12, 350, 24], 0, files.length); 

                          var progressTxt = progressWin.add("statictext", undefined, "Starting exporting files"); 

                          progressTxt.bounds = [0, 0, 340, 20]; 

                          progressTxt.alignment = "left"; 

                          progressWin.show(); 

                          // Progress bar ----------------------------------------------------------------------------------- 

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

                            var currentFile = files[i]; 

                             

                            try { 

                             var doc = app.open(currentFile, false); 

                             var docName = doc.name; 

                             // Progress bar ----------------------------------------------------------------------------------- 

                             progressBar.value = count; 

                             progressTxt.text = String("Exporting file - " + docName + " (" + count + " of " + files.length + ")"); 

                             // Progress bar ----------------------------------------------------------------------------------- 

                             var file = new File(pdfFolder + "/" + GetFileName(docName) + ".pdf"); 

                             if (file.exists) { 

                              var increment = 1; 

                              while (file.exists) { 

                               file = new File(pdfFolder + "/" + GetFileName(docName) + "(" + increment++ + ").pdf"); 

                              } 

                             } 

                             

                             doc.exportFile(ExportFormat.pdfType, file, false, selectedPdfPreset); 

                             doc.close(SaveOptions.NO); 

                             count++; 

                            } 

                            catch(e) {} 

                          // Progress bar ----------------------------------------------------------------------------------- 

                          progressWin.close(); 

                          // Progress bar ----------------------------------------------------------------------------------- 

                          alert("Done.", gScriptName + " - " + gScriptVersion); 

                          //---------------------------------------------------------------------------------------- ---------------------------------------------------------------- 

                          function VerifyFolder(folder) { 

                          if (!folder.exists) { 

                            var folder = new Folder(folder.absoluteURI); 

                            var array1 = new Array(); 

                            while (!folder.exists) { 

                             array1.push(folder); 

                             folder = new Folder(folder.path); 

                            } 

                            var array2 = new Array(); 

                            while (array1.length > 0) { 

                             folder = array1.pop(); 

                             if (folder.create()) { 

                              array2.push(folder); 

                             } else { 

                              while (array2.length > 0) { 

                               array2.pop.remove(); 

                              } 

                              throw "Folder creation failed"; 

                             }  

                            } 

                          //---------------------------------------------------------------------------------------- ---------------------------------------------------------------- 

                          function GetFiles(theFolder) {   

                              var file;   

                                  fileList = theFolder.getFiles();   

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

                                  file = fileList[i];  

                                  if(file.name==".DS_Store") continue; 

                                if (file instanceof Folder) {   

                                    if(flag==0){   

                                      flag=1;   

                                      GetFiles(file);   

                                      }   

                                  }   

                                else if (file instanceof File && file.name.match(/\.indd$/i)) {   

                                 files.push(file);   

                                }   

                              }   

                              return files;   

                              }  

                          //---------------------------------------------------------------------------------------- ---------------------------------------------------------------- 

                          function GetFileName(fileName) { 

                          var string = ""; 

                          var result = fileName.lastIndexOf("."); 

                          if (result == -1) { 

                            string = fileName; 

                          else { 

                            string = fileName.substr(0, result); 

                          return string; 

                          //---------------------------------------------------------------------------------------- ---------------------------------------------------------------- 

                          function GetSettings() { 

                          var set = eval(app.extractLabel("Kas_" + gScriptName + gScriptVersion)); 

                          if (set == undefined) { 

                            set = { selectedPdfPresetIndex : 0 }; 

                          return set; 

                          }

                          • 10. Re: How to get subfolder indesign files
                            Johnwhite Level 1

                            thank you Kai

                             

                            yes, I think so,

                             

                            so, How to change?

                             

                            regard

                            John

                            • 11. Re: How to get subfolder indesign files
                              Kai Rübsamen Adobe Community Professional

                              Try this one! I changed only the parts how subfolders are handled

                               

                              var gScriptName = "Batch export INDD-files to PDF"; // Name of the script  
                              var gScriptVersion = "1.0"; // Version  
                              var gSet = GetSettings();  
                              var flag=0, files = [];  
                              CreateDialog();  
                              
                              //===================================== FUNCTIONS  ======================================  
                              function CreateDialog() {  
                                  var pdfPresetsList =app.pdfExportPresets.everyItem().name;   
                                  var win = new Window ("dialog", gScriptName + " - " + gScriptVersion);  
                                  win.p = win.add("panel", undefined, "");  
                                  win.p.alignChildren = "right";  
                                  win.p.alignment = "fill";      
                                  win.p.g = win.p.add("group");  
                                  win.p.g.st = win.p.g.add("statictext", undefined, "PDF-presets:");  
                                  win.p.g.ddl = win.p.g.add("dropdownlist", undefined, pdfPresetsList);  
                                  win.p.g.ddl.selection = gSet.selectedPdfPresetIndex;  
                                  win.p.g.ddl.preferredSize.width = 220;  
                                  win.buttons = win.add("group");  
                                  win.buttons.orientation = "row";     
                                  win.buttons.alignment = "center";  
                                  win.buttons.ok = win.buttons.add("button", undefined, "OK", {name:"ok"});  
                                  win.buttons.cancel = win.buttons.add("button", undefined, "Cancel", {name:"cancel"});  
                                  var showDialog = win.show();  
                                  if (showDialog == 1) {  
                                      gSet.selectedPdfPresetIndex = win.p.g.ddl.selection.index;  
                                      app.insertLabel("Kas_" + gScriptName + gScriptVersion, gSet.toSource());  
                                      Main();  
                                  }   
                              }  
                              
                              //---------------------------------------------------------------------------------------- ----------------------------------------------------------------  
                              function Main() {  
                                  var folder = Folder.selectDialog("Select a folder with InDesign files to export");  
                                  if (folder == null) exit();  
                                  var files = GetFiles(folder, []);  
                                  var pdfFolder = new Folder(folder.fsName + "/" + "Pdf");  
                                  VerifyFolder(pdfFolder);  
                                  if (files.length == 0) {  
                                      alert("No files to open.", gScriptName + " - " + gScriptVersion);  
                                      exit();  
                                  }  
                                  var pdfPresets =app.pdfExportPresets.everyItem().getElements();  
                                  var selectedPdfPreset = pdfPresets[gSet.selectedPdfPresetIndex];  
                                  var count = 1;  
                                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;  
                                  // Progress bar -----------------------------------------------------------------------------------  
                                  var progressWin = new Window ("window", gScriptName + " - " + gScriptVersion);  
                                  var progressBar = progressWin.add ("progressbar", [12, 12, 350, 24], 0, files.length);  
                                  var progressTxt = progressWin.add("statictext", undefined, "Starting exporting files");  
                                  progressTxt.bounds = [0, 0, 340, 20];  
                                  progressTxt.alignment = "left";  
                                  progressWin.show();  
                                  // Progress bar -----------------------------------------------------------------------------------  
                                  for (var i = 0; i < files.length; i++) {  
                                      var currentFile = files[i];              
                                      try {  
                                       var doc = app.open(currentFile, false);  
                                       var docName = doc.name;  
                                       // Progress bar -----------------------------------------------------------------------------------  
                                       progressBar.value = count;  
                                       progressTxt.text = String("Exporting file - " + docName + " (" + count + " of " + files.length + ")");  
                                       // Progress bar -----------------------------------------------------------------------------------  
                                       var file = new File(pdfFolder + "/" + GetFileName(docName) + ".pdf");  
                                       if (file.exists) {  
                                          var increment = 1;  
                                          while (file.exists) {  
                                           file = new File(pdfFolder + "/" + GetFileName(docName) + "(" + increment++ + ").pdf");  
                                          }  
                                       }  
                                          
                                       doc.exportFile(ExportFormat.pdfType, file, false, selectedPdfPreset);  
                                       doc.close(SaveOptions.NO);  
                                       count++;  
                                      }  
                                      catch(e) {}  
                                  }  
                                  // Progress bar -----------------------------------------------------------------------------------  
                                  progressWin.close();  
                                  // Progress bar -----------------------------------------------------------------------------------  
                                  alert("Done.", gScriptName + " - " + gScriptVersion);  
                              }  
                              
                              //---------------------------------------------------------------------------------------- ----------------------------------------------------------------  
                              function VerifyFolder(folder) {  
                                  if (!folder.exists) {  
                                      var folder = new Folder(folder.absoluteURI);  
                                      var array1 = new Array();  
                                      while (!folder.exists) {  
                                       array1.push(folder);  
                                       folder = new Folder(folder.path);  
                                      }  
                                      var array2 = new Array();  
                                      while (array1.length > 0) {  
                                       folder = array1.pop();  
                                       if (folder.create()) {  
                                          array2.push(folder);  
                                       } else {  
                                          while (array2.length > 0) {  
                                           array2.pop.remove();  
                                          }  
                                          throw "Folder creation failed";  
                                       }   
                                      }  
                                  }  
                              }  
                              
                              //---------------------------------------------------------------------------------------- ----------------------------------------------------------------  
                              function GetFiles(dir, list) {    
                                  var f = Folder (dir).getFiles ('*.*');;    
                                  for (var i = 0; i < f.length; i++) {    
                                      if (f[i] instanceof Folder) {    
                                          GetFiles(f[i], list);    
                                      }    
                                      else if (f[i].name.match(/\.indd$/i)) {    
                                          list.push(f[i]);    
                                      }    
                                  }    
                                  return list;    
                              }   
                              //---------------------------------------------------------------------------------------- ----------------------------------------------------------------  
                              function GetFileName(fileName) {  
                                  var string = "";  
                                  var result = fileName.lastIndexOf(".");  
                                  if (result == -1) {  
                                      string = fileName;  
                                  }  
                                  else {  
                                      string = fileName.substr(0, result);  
                                  }  
                                  return string;  
                              }  
                              //---------------------------------------------------------------------------------------- ----------------------------------------------------------------  
                              function GetSettings() {  
                                  var set = eval(app.extractLabel("Kas_" + gScriptName + gScriptVersion));  
                                  if (set == undefined) {  
                                      set = { selectedPdfPresetIndex : 0 };  
                                  }  
                                  return set;  
                              }  
                              
                              • 12. Re: How to get subfolder indesign files
                                Johnwhite Level 1

                                Hi Kai

                                 

                                My goal is only the target folder and the subfolders

                                but not sub-subfolder

                                 

                                so it isn't my goal.

                                 

                                any way thank you so much.

                                 

                                Regard

                                John

                                • 13. Re: How to get subfolder indesign files
                                  natrev Level 3

                                  Hi John,

                                   

                                  Try this code..

                                   

                                  The Final PDF files stored in Desktop / PDF folder

                                   

                                  //======================================================================================
                                  var gScriptName = "Batch export INDD-files to PDF"; // Name of the script
                                  var gScriptVersion = "1.0"; // Version
                                  var gSet = GetSettings();
                                  var flag=0,files = [];
                                  var fileLs=[];
                                  CreateDialog();
                                  //===================================== FUNCTIONS  ======================================
                                  function CreateDialog() {
                                  var pdfPresetsList =app.pdfExportPresets.everyItem().name; 
                                  var win = new Window("dialog", gScriptName + " - " + gScriptVersion);
                                  win.p = win.add("panel", undefined, "");
                                  win.p.alignChildren = "right";
                                  win.p.alignment = "fill";
                                  
                                  
                                  win.p.g = win.p.add("group");
                                  win.p.g.st = win.p.g.add("statictext", undefined, "PDF-presets:");
                                  win.p.g.ddl = win.p.g.add("dropdownlist", undefined, pdfPresetsList);
                                  win.p.g.ddl.selection = gSet.selectedPdfPresetIndex;
                                  win.p.g.ddl.preferredSize.width = 220;
                                  win.buttons = win.add("group");
                                  win.buttons.orientation = "row";   
                                  win.buttons.alignment = "center";
                                  win.buttons.ok = win.buttons.add("button", undefined, "OK", {name:"ok" });
                                  win.buttons.cancel = win.buttons.add("button", undefined, "Cancel", {name:"cancel"});
                                  var showDialog = win.show();
                                  if (showDialog == 1) {
                                    gSet.selectedPdfPresetIndex = win.p.g.ddl.selection.index;
                                    app.insertLabel("Kas_" + gScriptName + gScriptVersion, gSet.toSource());
                                    Main();
                                  } 
                                  }
                                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                                  function Main() {
                                  var folder = Folder.selectDialog("Select a folder with InDesign files to export");
                                  if (folder == null) exit();
                                  var files = GetFiles(folder);
                                  var pdfFolder = new Folder(Folder.desktop+ "/" + "PDF");
                                  if (files.length == 0) {
                                    alert("No files to open.", gScriptName + " - " + gScriptVersion);
                                    exit();
                                  }
                                  if(!pdfFolder.exists)pdfFolder.create();
                                  //VerifyFolder(pdfFolder);
                                  var pdfPresets =app.pdfExportPresets.everyItem().getElements();
                                  var selectedPdfPreset = pdfPresets[gSet.selectedPdfPresetIndex];
                                  var count = 1;
                                  app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                                  // Progress bar -----------------------------------------------------------------------------------
                                  var progressWin = new Window ("window", gScriptName + " - " + gScriptVersion);
                                  var progressBar = progressWin.add ("progressbar", [12, 12, 350, 24], 0, files.length);
                                  var progressTxt = progressWin.add("statictext", undefined, "Starting exporting files");
                                  progressTxt.bounds = [0, 0, 340, 20];
                                  progressTxt.alignment = "left";
                                  progressWin.show();
                                  // Progress bar -----------------------------------------------------------------------------------
                                  for (var i = 0; i < files.length; i++) {
                                    var currentFile = files[i];
                                    
                                    try {
                                     var doc = app.open(currentFile, false);
                                     var docName = doc.name;
                                     // Progress bar -----------------------------------------------------------------------------------
                                     progressBar.value = count;
                                     progressTxt.text = String("Exporting file - " + docName + " (" + count + " of " + files.length + ")");
                                     // Progress bar -----------------------------------------------------------------------------------
                                     var file = new File(pdfFolder + "/" + GetFileName(docName) + ".pdf");
                                     if (file.exists) {
                                      var increment = 1;
                                      while (file.exists) {
                                       file = new File(pdfFolder + "/" + GetFileName(docName) + "(" + increment++ + ").pdf");
                                      }
                                     }
                                    
                                     doc.exportFile(ExportFormat.pdfType, file, false, selectedPdfPreset);
                                     doc.close(SaveOptions.NO);
                                     count++;
                                    }
                                    catch(e) {}
                                  }
                                  // Progress bar -----------------------------------------------------------------------------------
                                  progressWin.close();
                                  // Progress bar -----------------------------------------------------------------------------------
                                  alert("Done.", gScriptName + " - " + gScriptVersion);
                                  }
                                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                                  function VerifyFolder(folder) {
                                  if (!folder.exists) {
                                    var folder = new Folder(folder.absoluteURI);
                                    var array1 = new Array();
                                    while (!folder.exists) {
                                     array1.push(folder);
                                     folder = new Folder(folder.path);
                                    }
                                    var array2 = new Array();
                                    while (array1.length > 0) {
                                     folder = array1.pop();
                                     if (folder.create()) {
                                      array2.push(folder);
                                     } else {
                                      while (array2.length > 0) {
                                       array2.pop.remove();
                                      }
                                      throw "Folder creation failed";
                                     } 
                                    }
                                  }
                                  }
                                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                                  function GetFiles(theFolder) {  
                                      var file,filesub;  
                                          fileList = theFolder.getFiles();  
                                      for (i = 0; i < fileList.length; i++) {  
                                          file = fileList[i]; 
                                          if(file.name==".DS_Store") continue;
                                          if (file instanceof Folder) {  
                                            fileLs = file.getFiles();  
                                            alert(fileLs.length);
                                              for (j = 0; j < fileLs.length; j++) {  
                                                  filesub = fileLs[j]; 
                                                  if (filesub instanceof File && filesub.name.match(/\.indd$/i)) {  
                                                     files.push(filesub);  
                                                  }
                                              }  
                                          }
                                        else if (file instanceof File && file.name.match(/\.indd$/i)) {  
                                         files.push(file);  
                                        }  
                                      }  
                                      return files;  
                                      } 
                                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                                  function GetFileName(fileName) {
                                  var string = "";
                                  var result = fileName.lastIndexOf(".");
                                  if (result == -1) {
                                    string = fileName;
                                  }
                                  else {
                                    string = fileName.substr(0, result);
                                  }
                                  return string;
                                  }
                                  //---------------------------------------------------------------------------------------- ----------------------------------------------------------------
                                  function GetSettings() {
                                  var set = eval(app.extractLabel("Kas_" + gScriptName + gScriptVersion));
                                  if (set == undefined) {
                                    set = { selectedPdfPresetIndex : 0 };
                                  }
                                  return set;
                                  }
                                  
                                  • 14. Re: How to get subfolder indesign files
                                    Johnwhite Level 1

                                    thank you guys, thank you so much.

                                     

                                    appreciate.

                                     

                                    Regard

                                    John