12 Replies Latest reply on Mar 10, 2017 3:56 PM by geordiem74078114

    Folder.getFiles Multiple Filters Mask

    canpserra

      Hi all,

       

      Folder.getFiles (".*") ... in this case it will get all types of files

       

      Folder.getFiles (".jpg") ... in this case it will get all files with .jpg extension

       

      ... but how can I filter to get (for example) all files with ".jpg", " .tif" or ".eps" extension ?

       

      ESTK help says: A mask string can also be the name of a function that takes a File or Folder object as its argument. It is called for each file or folder found in the search; if it returns true, the object is added to the return array.

       

      Any idea of how to make such a function?

       

      I will appreciate any help.

        • 1. Re: Folder.getFiles Multiple Filters Mask
          Fred Goldman Level 3

          Send an array:

           

          Folder.getFiles (["Files:*.doc;*.docx;*.rtf"])

           

          Edit: That should be:

           

          Folder.getFiles ("Please select files",["Files:*.doc;*.docx;*.rtf"])

          • 2. Re: Folder.getFiles Multiple Filters Mask
            moluapple Level 4

            Use regex.

            Folder.getFiles(/.+\.(?:gif|jpe?g|[ew]mf|eps|tiff?|psd|pdf|bmp|png)$/i);

            • 3. Re: Folder.getFiles Multiple Filters Mask
              Muppet Mark Level 5

              You can use a regular expression…

               

              var pics = Folder.desktop.getFiles( /\.(epsf?|jpe?g|tiff?)$/i );

               

              $.write( pics.join( '\r' ) );

              • 4. Re: Folder.getFiles Multiple Filters Mask
                [Jongware] Most Valuable Participant

                It depends on your operating system!

                 

                From a recent script of mine:

                 

                if (File.fs == "Windows")

                pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', "Adobe PDF:*.pdf,All files:*.*", false);

                else

                pdfFile = Folder.myDocuments.openDlg( 'Load PDF document', function(file) { return file instanceof Folder || (!(file.hidden) && (file.name.match(/\.pdf$/i) || file.type == "PDF ")); }, false );

                 

                (Note the second part for OS X. Since I know the 'type' of a PDF is "PDF " -- including a space at the end --, this ought to be enough to discern PDFs. However, it's possible the OS File Type might be 'unknown' (for example, with a downloaded file), so I test the extension anyway.)

                • 5. Re: Folder.getFiles Multiple Filters Mask
                  Gonterman1201 Level 1

                  What would you use to just return the file name and not the full name?

                  • 6. Re: Folder.getFiles Multiple Filters Mask
                    Gonterman1201 Level 1

                    Folder.getFiles(File.name ); but this does not work. Any ideas?

                    • 7. Re: Folder.getFiles Multiple Filters Mask
                      Laubender Adobe Community Professional & MVP

                      @Gonterman1201 – for me on Mac OSX 10.6.8 it is working like that:

                       

                      Presume there is a folder named "Test" in the top folder of my internal harddisk and one or some files with suffix "jpg" in it.

                       

                      var myFolder = Folder("/"+"Test");
                      var myJPGFilesArray = myFolder.getFiles("*.jpg");
                      
                      if(myJPGFilesArray !== null){
                          for(var n=0; n<myJPGFilesArray.length;n++){
                              $.writeln(myJPGFilesArray[n].name);
                              };
                          };
                      

                       

                      It gets all files with suffix "jpg" in the folder "Test"…

                       

                      getFiles() returns an Array of files and subfolders.

                      Or null (if there are no files or subfolders)…

                       

                      Just another example with a filter for a specific file name:

                       

                      //To filter to a specific file name:
                      
                      var myFileName = "NewSWFPosterImage.psd";
                      
                      var myFolder = Folder("/"+"Test");
                      var myFileArray = myFolder.getFiles(myFileName);
                      
                      if(myFileArray !== null){
                          var myFileFound = myFileArray[0];
                          $.writeln(myFileFound.name);
                          };
                      

                       

                      Uwe

                      • 8. Re: Folder.getFiles Multiple Filters Mask
                        Arie Stavchansky Level 2

                        @Jongware, your solution for file masking didn't work for me on the MacOS for After Effects.  Do you know of any differences between the two apps?  I'm actually trying to let all file types be selectable in an open dialog.  Any suggestions? 

                        • 9. Re: Re: Folder.getFiles Multiple Filters Mask
                          [Jongware] Most Valuable Participant

                          Arie,

                          Unfortunately I cannot test with After Effects. This works in InDesign:

                           

                          Folder.myDocuments.openDlg( 'Load any document', function(file) { return true; }, false );
                          

                           

                          and I can select any file I want.

                           

                          If it does not work in AE, then the Javascript Base Classes are not as "shared" across all applications of The Suite as Adobe made us believe.

                          • 10. Re: Re: Folder.getFiles Multiple Filters Mask
                            Laubender Adobe Community Professional & MVP

                            @Arie – what version of After Effects?
                            Just tested Jongware's code snippet with After Effects CS5.5 v10.5.0.253 on Mac OSX 10.7.5
                            Worked as expected.

                             

                            However running the script from the ESTK on InDesign or After Effects reveals a small difference between the two:

                             

                            If After Effect's own "New Composition" dialog is open and waiting for user input the script will be executed after the user has answered "New Composition".

                             

                            Opposed to the behaviour using InDesign: If InDesign's "New Document" dialog is already open and not answered by the user, openDlg will be executed immediately. And if the user of the ESTK chooses a file InDesign will throw an error: "A modal dialog or message is active* " (waiting for user input).

                             

                            Not so with After Effects. After Effects will wait until the user is answering "New Composition" and then present openDlg().

                             

                            Uwe

                             

                            *Roughly translated from my German version.

                            • 11. Re: Re: Folder.getFiles Multiple Filters Mask
                              Arie Stavchansky Level 2

                              I was testing against CC.  Jongware's code does in fact work, but I get a strange behavior if I use it on .openDialog() and not .openDlg().   I eventually figured out a method that allowed me to use .openDlg().  Thank you @Jongware for providing the code.  I appreciate it!

                              • 12. Re: Re: Folder.getFiles Multiple Filters Mask
                                geordiem74078114 Level 1

                                For future travelers I was having trouble using a string as my regex expression for Folder.getFiles file masking.

                                 

                                Here's what worked for me:

                                var FO = new Folder(path);
                                
                                // Literal 
                                var fileObjs = FO.getFiles(/\.(wav|wma)$/i);
                                
                                // From String
                                var mask = "\\.(wav|wma)$";
                                var re = new RegExp(mask, "i");
                                 var fileObjs = FO.getFiles(re);