8 Replies Latest reply on Jun 23, 2016 9:10 AM by 1CM69

    Alert user to items outside of Array in Extendscript

    1CM69 Level 1

      I have a script that checks a folder for image files of certain format contained within an array.

      I need to be able to alert the user if:

      1. a folder they choose contain any file type not included in the array (illegal file type).
      2. the folder they choose contains more than one file type included in the array (mixed legal file types).

      or

      1. the folder they choose contains any file types not in the array plus a file type that is in the array (mix of illegal & legal file types).

      Here is the code that checks through a folder for certain file types.

      var DARKfileList = [], FILE_TYPE, FTlen, i, SEARCH_MASK;
      FILE_TYPE = ["orf", "tif", "tiff", "jpg", "jpeg"];
      FTlen = FILE_TYPE.length;
      for (i = 0; i < FTlen; i++) {
          SEARCH_MASK = "*." + FILE_TYPE[i];
          DARKfileList = DARKfileList.concat(DARKinputFolder.getFiles(SEARCH_MASK));
      }
      alert('found: ' + DARKfileList.length + ' files');
      

       

      At the moment I have sort of achieved part 1 but it's messy as it simply checks if DARKfileList is empty. I can use this to alert the user that the folder they choose in empty but I would much prefer a separate check for 'illegal file types' This is the check for empty list:

       

      if (DARKfileList.length == 0)
      {
          alert('Folder does not contain allowed file types!','DARK FRAME folder error');
      } 
      

       

      Regards..,

        • 1. Re: Alert user to items outside of Array in Extendscript
          JJMack Most Valuable Participant

          1CM69 wrote:

           

          At the moment I have sort of achieved part 1 but it's messy as it simply checks if DARKfileList is empty. I can use this to alert the user that the folder they choose in empty but I would much prefer a separate check for 'illegal file types' This is the check for empty list:

          Regards..,

          Why?  You just message the user their folder contain no image file types your script is designed to process. Why do you care if the user has other type file  in their folder that your script is not designed to process what make them illegal.  Are programs file, text files, .AI file etc  "illegal" files or are they legal files your script is not designed to process.   You could have no mask look at all files and tell the user that they have illegal file(s) when your script can not process a file  it see is in the folder.

          • 2. Re: Alert user to items outside of Array in Extendscript
            1CM69 Level 1

            Thanks for replying, I can live with my solution to part 1 it is the other two parts I am stuck on really.

             

            Regards..,

            • 3. Re: Alert user to items outside of Array in Extendscript
              Chuck Uebele Adobe Community Professional & MVP

              Try this script I think all my logic is correct...then again....:

               

              #target photoshop
              
              var fileTypes = ['TIF', 'TIFF', 'JPG', 'JPEG','PSD','RAF'];//keep uppercase
              var fileList = new Array();
              var legalTypes = new Array();
              var illegalTypes = new Array();
              var folderToUse = Folder.selectDialog ('Select a folder');
              
              var searchMask = '*.*'
              fileList = folderToUse.getFiles(searchMask)
              checkFiles (fileList);
              
              if(legalTypes.length==1&& illegalTypes.length ==0){alert('only one file type of legal files are in folder.\nNo illegal file types')}
              if(legalTypes.length>1&& illegalTypes.length ==0){alert('More than one legal file types.\nNo illegal file types.')}
              if(legalTypes.length==1&& illegalTypes.length > 0){alert('only one legal file type.\nContains illegal file types.')}
              if(legalTypes.length>1&& illegalTypes.length > 0){alert('More than one legal file type.\nContains illegal file types.')}
              if(legalTypes.length==0 && illegalTypes.length > 0){alert('No legal file types.\nContains illegal file types.')}
              
              function checkFiles(fileArray){
                  if(fileArray.length>0){
                      for(var i =0;i<fileArray.length;i++){
                          var fileExpand = fileArray[i].toString().split('.')
                          var ext = fileExpand[fileExpand.length-1].toUpperCase();
                          var isLegal = false;
                          for(var j = 0;j<fileTypes.length;j++){
                              if(ext == fileTypes[j]){
                                  isLegal = true;
                                  if(legalTypes.length ==0){legalTypes.push(ext)}
                                  else{
                                      var dupeExt = false
                                      for(var k=0;k<legalTypes.length;k++){
                                          if(ext == legalTypes[k]){
                                              dupeExt = true
                                              }//end if for chucking extension against existing file types.
                                          }//end loop for checking to see if extension exist in array already
                                      if(!dupeExt){legalTypes.push(ext)}
                                      }//end else for first extension in array
                                  }//end if for checking file types
                              }//end loop to check ext
                              if(ext !='XMP' && ext !='BRIDGESORT' && ext != 'DB' && !isLegal){illegalTypes.push(ext)}       
                          }//end loop to get ext
                      }//end if
                  else{alert('No files are in the folder')}
                  }
              
              • 4. Re: Alert user to items outside of Array in Extendscript
                1CM69 Level 1

                Hi Chuck,

                 

                this code seems to work fine except for one small stumbling block.

                 

                If I have a folder which contains a 'legal' file type, it passes the test but if this same folder has another folder inside it and this second sub-folder can be empty the code picks up that this empty sub-folder is an 'illegal' file type.

                 

                It should in fact ignore sub-folders.

                 

                Is there a fix for this?

                 

                Regards..,

                • 5. Re: Alert user to items outside of Array in Extendscript
                  Chuck Uebele Adobe Community Professional & MVP

                  I'll have to see. This seems a new behavior, as before it use to only get files at the top level of a folder. Perhaps add some code to check the parent name of the files, so that if it doesn't match the folder selected, exclude that file.

                  • 6. Re: Alert user to items outside of Array in Extendscript
                    1CM69 Level 1

                    Hi Chuck,

                     

                    I am testing the code exactly as you have posted it.

                     

                    Example 1.

                     

                    Untitled Parent Folder > Untitled.jpg

                     

                    Returning:

                    legalTypes.length==1&& illegalTypes.length ==0 - only one file type of legal files are in folder.\nNo illegal file types
                    

                     

                    Example 2.

                     

                    Untitled Parent Folder > Untitled.jpg

                    Empty Sub Folder

                     

                    Returning:

                    legalTypes.length==1&& illegalTypes.length > 0 - only one legal file type.\nContains illegal file types
                    

                     

                    Hope this info helps.

                     

                    Regards..,

                    • 7. Re: Alert user to items outside of Array in Extendscript
                      Chuck Uebele Adobe Community Professional & MVP

                      It's weird, It wasn't doing the subfolder thing this morning, when I tried, but I added a few lines to make sure the files are only from the parent folder.

                       

                      #target photoshop
                      
                      var fileTypes = ['TIF', 'TIFF', 'JPG', 'JPEG','PSD','RAF'];//keep uppercase
                      var allFiles = new Array()
                      var fileList = new Array();
                      var legalTypes = new Array();
                      var illegalTypes = new Array();
                      var folderToUse = Folder.selectDialog ('Select a folder');
                      var searchMask = '*.???*'
                      allFiles = folderToUse.getFiles(searchMask);
                      for(var i = 0;i<allFiles.length;i++){
                          if(allFiles[i].parent.name == folderToUse.name){fileList.push(allFiles[i])}
                          }
                      checkFiles (fileList);
                      
                      if(legalTypes.length==1&& illegalTypes.length ==0){alert('only one file type of legal files are in folder.\nNo illegal file types')}
                      if(legalTypes.length>1&& illegalTypes.length ==0){alert('More than one legal file types.\nNo illegal file types.')}
                      if(legalTypes.length==1&& illegalTypes.length > 0){alert('only one legal file type.\nContains illegal file types.')}
                      if(legalTypes.length>1&& illegalTypes.length > 0){alert('More than one legal file type.\nContains illegal file types.')}
                      if(legalTypes.length==0 && illegalTypes.length > 0){alert('No legal file types.\nContains illegal file types.')}
                      
                      function checkFiles(fileArray){
                          if(fileArray.length>0){
                              for(var i =0;i<fileArray.length;i++){
                                  var fileExpand = fileArray[i].toString().split('.')
                                  var ext = fileExpand[fileExpand.length-1].toUpperCase();
                                  var isLegal = false;
                                  for(var j = 0;j<fileTypes.length;j++){
                                      if(ext == fileTypes[j]){
                                          isLegal = true;
                                          if(legalTypes.length ==0){legalTypes.push(ext)}
                                          else{
                                              var dupeExt = false
                                              for(var k=0;k<legalTypes.length;k++){
                                                  if(ext == legalTypes[k]){
                                                      dupeExt = true
                                                      }//end if for chucking extension against existing file types.
                                                  }//end loop for checking to see if extension exist in array already
                                              if(!dupeExt){legalTypes.push(ext)}
                                              }//end else for first extension in array
                                          }//end if for checking file types
                                      }//end loop to check ext
                                      if(ext !='XMP' && ext !='BRIDGESORT' && ext != 'DB' && !isLegal){illegalTypes.push(ext)}       
                                  }//end loop to get ext
                              }//end if
                          else{alert('No files are in the folder')}
                          }
                      
                      1 person found this helpful
                      • 8. Re: Alert user to items outside of Array in Extendscript
                        1CM69 Level 1

                        Perfect, works like a dream.

                         

                        Thanks Chuck