22 Replies Latest reply on Mar 30, 2012 1:33 PM by Muppet Mark

    find and open files in subfolders




      Simple request I know but I can't manage to find how to make fileArray = targetFolder.getFiles("*.indd"); to access *.indd files inside sub folders and perhaps in sub sub folders..


      Can anyone share how to do this?


      Thanks in advance, Ken

        • 1. Re: find and open files in subfolders
          Kasyan Servetsky Rockstar

          Here is an example -- this script gets all indesign documents from the selected folder including its subfolders and puts all them into a book.

          var myFolder = Folder.selectDialog( "Select a folder with InDesign files" );
          if ( myFolder != null ) {
               var myFiles = [];
               if ( myFiles.length > 0 ) {
                    var myBookFileName = myFolder + "/"+ myFolder.name + ".indb";
                    myBookFile = new File( myBookFileName );
                    if ( myBookFile.exists ) {
                         if ( app.books.item(myFolder.displayName + ".indb") == null ) {
                              myBook = app.open( myBookFile );
                    else {
                          myBook = app.books.add( myBookFile );
                          myBook.automaticPagination = false;
                          for ( i=0; i < myFiles.length; i++ ) {
                              myBook.bookContents.add( myFiles[i] );
          //=================================== FUNCTIONS =========================================
          function GetSubFolders(theFolder) {
               var myFileList = theFolder.getFiles();
               for (var i = 0; i < myFileList.length; i++) {
                    var myFile = myFileList[i];
                    if (myFile instanceof Folder){
                    else if (myFile instanceof File && myFile.name.match(/\.indd$/i)) {

          Hope this helps.



          • 2. Re: find and open files in subfolders
            [Jongware]-9BC6tI Champion

            A slight clarification on Kasyan's script:


            Contrary to what you are thinking, your own getFiles command will return Folders ... if its name ends with .indd, that is! It's just doing exactly to the letter what you asked for. And it's a mean trick to scripters to end a folder's name in INDD (I think a lot of my own scripts would fall over it, getting a folder when it really expected to be handed a file).


            So Kasyan checks "everything", 'cause that's the default when you don't give an argument to getFiles. Each individual item is first checked whether it's a folder or not, and if it is, his routine calls itself (a process called "recursion"; and its name doesn't contain "curse" for nothing). So even if the folder name ends with .INDD, this part of the routine will do the proper thing.  Only if it's not a folder, he checks if the name ends with ".indd", and if so, It Is A Document.


            (Note to Kasyan: uh, what about "MeanThingToDo.INDD"? A-ha -- your GREP match is case InSeNsItIvE. Almost got me there )

            • 3. Re: find and open files in subfolders
              kenjones99 Newcomer

              Thanks, that's got it.


              I'm learning Javascript but coming from Applescript.. I admit there are some better ways to do things but this is not one!


              -- tell finder to get ENTIRE contents of folder x whose name contains ".indd" -- is soo much simpler

              • 4. Re: find and open files in subfolders
                Muppet Mark-QAl63s Champion

                Whilst 'entire contents' is simpler to understand it has an overhead and is some distance slower with directories containing large quantities of files… The 'whose' filtering you will miss but there are posts here on how to add this functionality to JavaScript. At a glance only yesterday it looks like its in 'extendables' and 'marc' also posted one a while back too.

                1 person found this helpful
                • 5. Re: find and open files in subfolders
                  kenjones99 Newcomer

                  Understood Muppet Mark, it's a trade off between simplicity and speed.


                  I actually use OS X Spotlight to rapidly find .indd files on massive volumes as part of my Applescripts but want to be able to move to JS

                  • 6. Re: find and open files in subfolders
                    Muppet Mark-QAl63s Champion

                    I too use Spotlight a lot AppleScript… Kasyan's method is in the somewhere between these for performance but I would expect it to be some what closer to Spotlight…

                    • 7. Re: find and open files in subfolders

                      What a great example of when/how to use recursion! Thank-you to Jongware for spelling it out and Kasyan for scripting it. Previous to reading this post I had been using Command Prompt to write up tab separated dir lists and then I read those in for file queues. Haha things are looking a little less hacked on my end.

                      • 8. Re: find and open files in subfolders
                        Laubender Rockstar

                        @Jongware – And then we have the "strange" case of InDesign files without suffix on Mac OSX. Some users might think it would be a good idea to ommit it so they could easily identify the InDesign version in the file system. For several reasons NOT a good idea at all, but unfortunately possible…


                        See the following screengrab:



                        Just an idea, did not test that yet:

                        to detect those files, it could be a good idea to "misuse" an altered version of your "IDentify.jsx" script (thanks to that a lot!).


                        @For all see Jongware's "IDentify.jsx" at:



                        • 9. Re: find and open files in subfolders
                          [Jongware] Mythic

                          Uwe: I must admit I've somehow managed to save a file without the proper suffix on my Mac a couple of times -- much to my surprise, as I am so used to Windows' always adding one! I have no idea what the magic (or in this case, voodoo) sequence is that makes the Save dialog omit the suffix.


                          Sure, it is possible to test all suffix-less files with my script -- but ask youself the question "what is suffix-less?" What if you save an InDesign file as "my.test.file" -- surely the Mac will think the extension is ".file"? So you can't rely on this, unless you want to test each and every not-InDesign file ...

                          In addition, on Windows at least, adding ".indd" to a file name makes Windows think it is an InDesign file Then you should use my script on every file, even on .indd ones!


                          Oh enough with the fun. I see you still have your proper icons and "Art" descriptions on the Mac for all files. Wouldn't it be easier to test the Mac OS file type? Is that available through JS?  (I'd gladly try that out myself immediately, but am at work & behind Windows...)

                          • 10. Re: find and open files in subfolders
                            Laubender Rockstar

                            @Jongware – the point is, on Mac OSX all users could deliberately ommit the suffix if they choose "Save As" and delete the automatic generated suffix in the dialog (I did for testing reasons).


                            And you are right: if we like to catch those we have to test with an altered IDentify.jsx against ALL files. (Sigh.)


                            Beeing on the Mac maybe it's better to test by AppleScript to get the file information. But I'm not sure at all if that will work in every case. Maybe an AppleScript savvy participant could comment on that?



                            • 11. Re: find and open files in subfolders
                              absqua Champion

                              This stuff is extremely confusing on the Mac, to me anyway. Without the extension—which OS X uses to lookup the file's "Uniform Type Identifier," which will be associated with the highest installed version of InDesign—OS X will rely on the (now deprecated) file type and creator codes in an extended attribute to associate the file with an application. And that's if you save it from InDesign without an extension. If you remove the extension in the Finder, it will hide the extension but silently leave it there and continue to use the extension and UTI. (And if you then remove the extended attribute that holds the code, the Finder will unhide the extension!) Voodoo indeed. You could use an extension check along with a doScript() Applescript call inside a filter function for getFiles():


                              var folder = Folder.desktop,
                                  files = folder.getFiles(function(file) {
                                      var fT;
                                      if (file instanceof Folder) return false;        
                                      if (file.fsName.split(".").pop() === "indd") return true;
                                      fT = app.doScript("tell application \"Finder\"\ntry\nreturn the file type of (POSIX file \"" + file.fsName + "\" as alias)\non error\nreturn \"\"\nend try\nend tell", ScriptLanguage.APPLESCRIPT_LANGUAGE);
                                      if (typeof fT === 'string' && fT.substring(0, 3) === "IDd") return true;
                                      return false;


                              But I bet it would be faster and less error-prone to just read enough bytes from the beginning of every file to determine whether or not it's an InDesign file, as you suggested.



                              • 12. Re: find and open files in subfolders
                                Muppet Mark Rockstar

                                Jeff, as Jong pointed out earlier you don't need to call out to AppleScript to get those mac identifiers they are available properties of the File Object…


                                var f = File.openDialog( 'Pick Me…', undefined, false );
                                alert( 'Is Indesign = ' + /InDn/.test( f.creator ) );
                                alert( 'Is CS' + Number( f.type.match( /\d$/ ) - 2 ) ); // My lazy doc only match
                                $.writeln( f.fsName ); // Gives full name even with ext OS hidden…
                                • 13. Re: find and open files in subfolders
                                  absqua Champion

                                  D'oh! I could've used that a time or two. Thanks. A better (and reasonably quick) filter function, then:


                                  var folder = Folder.desktop,
                                      files = folder.getFiles(function(file) {    
                                          return !(file instanceof Folder) && (file.fsName.split(".").pop() === "indd" || file.type.indexOf("IDd") === 0);
                                  • 14. Re: find and open files in subfolders
                                    Muppet Mark Rockstar

                                    I only have CS2 & CS5 so all betwixt & between is a best guess… CS5.5 is that still IDd7? I dunno… I don't have Lion either so this may be kitty litter in that?

                                    • 15. Re: find and open files in subfolders
                                      absqua Champion

                                      CS5.5 is "IDd2"! (Of course!)

                                      • 16. Re: find and open files in subfolders
                                        Muppet Mark Rockstar

                                        PM or link me one… I want to see…

                                        • 18. Re: find and open files in subfolders
                                          [Jongware] Mythic



                                          Screen Shot 2012-03-30 at 9.50.11 PM.png


                                          Now I'm a-shiver for the very first ID CS6 document to try this on ...

                                          • 19. Re: find and open files in subfolders
                                            absqua Champion

                                            Mark—That was kind of silly of me; of course the extended attributes don't survive the Dropbox upload/download. I don't know how I could get it to you with the metadata intact...

                                            • 20. Re: find and open files in subfolders
                                              Muppet Mark Rockstar

                                              I wasn't sure about that… Compress it…?

                                              • 21. Re: find and open files in subfolders
                                                absqua Champion

                                                Yep, sure—something (more than usually) wrong with my brain today. Here it is: http://dl.dropbox.com/u/422307/CS5.5.indd.zip

                                                • 22. Re: find and open files in subfolders
                                                  Muppet Mark Rockstar

                                                  Your not mistaken IDd2 whats that all about…