5 Replies Latest reply on Jan 25, 2013 3:37 AM by BEGINNER_X

    Open all .indd files from the selected folder

    BEGINNER_X Level 3

      Hi All,

       

      I want to open all .indd files from the selected folder.

       

       

      Trying script:

       

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

      var myFiles = [];

      var myFileList = myFolder.getFiles();

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

                var myFile = myFileList[i];

               myFile.name.match(/\.indd$/)

                myFiles.push(myFile);

                app.open(myFiles)

                }

          

       

      Can anyone give solution for me.

       

      Thanks in advance

      BEGINNER

        • 1. Re: Open all .indd files from the selected folder
          MrTIFF Level 3

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

          if (!myFolder)

            exit(0);

          var myFileList = myFolder.getFiles();

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

            var myFile = myFileList[i];

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

                              app.open(myFile);

                    }

          }

           

          Cheers,

          Stephen

          • 2. Re: Open all .indd files from the selected folder
            Kasyan Servetsky Level 5

            Here is an example of how to open all indd-files from the selected folder and its subfolders.

             

            var files;
            var folder = Folder.selectDialog("Select a folder with InDesign documents");
            if (folder != null) {
                files = GetFiles(folder);
                if (files.length > 0) {
                    // turn off warnings: missing fonts, links, etc.
                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
            
                    for (i = 0; i < files.length; i++) {
                        app.open(files[i]);
                    }
                    // turn on warnings
                    app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
                }
                else {
                    alert("Found no InDesign documents");
                }
            }
            
            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;
            }
            
            • 3. Re: Open all .indd files from the selected folder
              [Jongware] Most Valuable Participant

              There are a couple of conceptual errors in your script, which may be the reason you couldn't get it to work. This line

               

              myFile.name.match(/\.indd$/)

               

              does "nothing". It tries to find a match for the variable "myFile", but then it doesn't 'do' anything with the result -- there is no "logic" surrounding the statement. So the next line,

               

              myFiles.push(myFile);

               

              will always be executed. You probably meant something like

               

              if (myFile.name.match(/\.indd$/))

                 myFiles.push(myFile);

               

              .. Javascript does what it gets told and does not do "what you mean".Next, another error is this:

               

              app.open(myFiles)

               

              I had to check this, but you are right: app.open can open an array of files -- I only use it to open just one file at a time. However, you placed this statement inside the main loop! So for every file in the loop, InDesign attempts to open *all* files, again and again.

               

              With these changes, you can get your original script running properly.

               

              By the way, note that your question has been answered before. A nice in-depth discussion is in http://forums.adobe.com/thread/791888 -- of particular interest is the entire "when is a file an InDesign file" sub-discussion.

               

              Here is a line that works ... but I would advise you not to use it. Please try to fix your own script! Only if you understand the basic concepts, then you can advance to this:

               

              app.open(Folder(Folder.selectDialog( "Select a folder with InDesign files")).getFiles(function(f) { return f instanceof File && !f.hidden && (f.name.match(/\.indd$/i) || f.type.match(/^IDd/)); } ));
              
              • 4. Re: Open all .indd files from the selected folder
                [Jongware] Most Valuable Participant

                Kasyan Servetsky wrote:

                 

                Here is an example of how to open all indd-files from the selected folder and its subfolders. ...

                 

                I think I'm not going to try to do that in a single statement ...

                • 5. Re: Open all .indd files from the selected folder
                  BEGINNER_X Level 3

                  Respected Jongware/Kasyan/All

                   

                  Thanks for your immediate response....

                   

                   

                  I created script with the help of forum:

                   

                   

                  var myFolder = Folder.selectDialog("Choose the folder")

                   

                  if(myFolder.length==0)

                  {

                      alert("No Folder Selected")

                      exit(0)

                      }

                   

                   

                  var myFiles = myFolder.getFiles("*.indd")

                   

                  if(myFiles.length==0)

                  {

                      alert("No InDesign files in folder")

                      }

                   

                   

                  for(i=0; i<myFiles.length; i++)

                  {

                      myFile = myFiles[i]

                      app.open(myFile)

                      }

                   

                   

                  Thanks in advance

                  BEGINNER