0

I am trying to integrate an Array containing a list of image file types that can be used in my Extendscript, this is what I have:

DFgroup.DFBtn.onClick = function(){    
     DFdefault = new Folder(DARKinputFolder); //stores the current folder in case of cancel.    
     DFdefault = Folder.selectDialog('****Please Select The Folder Where Your DARK FRAMES Are Located****',DARKinputFolder);
     if ( DFdefault != null ) {
                    DARKinputFolder = DFdefault.fsName.toString()
                       DFgroup.DFlabel.text = DARKinputFolder;

if(DFdefault){DARKinputFolder = DFdefault}; //if cancel is hit, this statement becomes false, and myFolder remains untouched.    
    //if it's true, then the selected folder from the holdFolder is transferred to myfolder.   

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

//if (DARKfileList.length <= 0)
//{
//var FILE_TYPE = ".jpg"; // The type of files that this script works on -- you can change
//var SEARCH_MASK = "*" + FILE_TYPE; // Image file filter to find only those files
//var DARKfileList = DARKinputFolder.getFiles(SEARCH_MASK);
//}


 if (DARKfileList.length == 0)
{
    alert('The DARK FRAME folder you have chosen is either empty or does not contain image files of the correct type.\r\rPlease locate & select the correct image folder.','DARK FRAME folder error');
    DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
    DFdefault = '';
    DFgroup.DFlabelQ.text = '';
    }
else{                      
                       DFgroup.DFlabelQ.text = '[' + DARKfileList.length + ']';
                       var DFCurrFolder = (new File(DARKinputFolder)).fsName;
                       DFgroup.DFlabel.helpTip = 'Location of files to process:\r' + DFCurrFolder;
                       FolderChecker()
     }}
     } //opens the folder browse UI using you main folder.

When a user hits the 'DFBtn' an open folder dialog appears and they can choose a folder that contains images.

This folder path along with the number of images contained within it are retrieved and output as text label on a dialog box.

The problem is that through testing and choosing a folder that does contain an image and that image is of the allowed type, the script goes straight to the 'Alert' message stating that the "folder you have chosen is either empty....." etc..

Now if I alter a small section of the code as:

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

The code now no longer goes straight to the 'Alert' message even if I deliberately choose a folder that is empty.


UPDATE

To aid in finding out what is going on I have added an extra line of code:

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

By just adding this line alert (SEARCH_MASK); straight away I had an insight to what is occurring.

Yes the FILE_TYPE Array is being iterated through but then the SEARCH_MASK is formed using just the last FILE_TYPE in the array, so in the case above the SEARCH_MASK is always created as:

*.jpeg

Therefore it will only be looking for files of that type within the chosen folder.

This is not what I am trying to achieve.

I want to know if the chosen folder contains ANY files of ANY of the types listed in the array.

For instance, the chosen folder could contain all *.tif files or even one of each file type.

How can I achieve what I require the code to do?

Regards..,


UPDATE 2 BOILED DOWN SCRIPT

Here's a boiled down script as requested:

var DARKfileList = '';

var AllowedFileTypes = Array( "orf", "tif", "tiff", "jpg", "jpeg" );



var dlgM = new Window('dialog','***###***###',undefined,{closeButton:false});
dlgM.center();
var DARKinputFolder = new Folder('/C/'); //create a folder to have the dialog start on. 
var DFdefault = new Folder(); //create a holding folder for canceling.

DFg = dlgM.add('panel {text:"DARK FRAMES",preferredSize:[450,15]}');
DFg.margins = [5,10,5,10];
DFg.alignChildren = 'left';
DFgroup = DFg.add('group');
DFgroup.DFBtn = DFgroup.add('button{text:"Select Folder...",preferredSize:[100,25]}');
DFgroup.DFBtn.helpTip = 'Please select your DARK FRAMES folder.';
DFgroup.DFlabel = DFgroup.add( 'statictext', undefined, DARKinputFolder, { truncate:'middle' } );
DFgroup.DFlabel.helpTip = 'Location of files to process';
DFgroup.DFlabel.preferredSize.width = '275';
DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
DFgroup.DFlabelQ = DFgroup.add( 'statictext', undefined, '');
DFgroup.DFlabelQ.helpTip = 'Number of DARK FRAMES to process';
DFgroup.DFlabelQ.preferredSize.width = '40';
DFgroup.DFlabelQ.text = '';

BTNg = dlgM.add('group {alignment: "center"}');
BTNg.ExitBtn = BTNg.add('button{text:"Exit",preferredSize:[105,25]}');
BTNg.ProcessBtn = BTNg.add('button{text:"Process",preferredSize:[105,25]}');
BTNg.ProcessBtn.enabled = false;

DFgroup.DFBtn.onClick = function(){    
     DFdefault = new Folder(DARKinputFolder); //stores the current folder in case of cancel.    
     DFdefault = Folder.selectDialog('****Please Select The Folder Where Your DARK FRAMES Are Located****',DARKinputFolder);
     if ( DFdefault != null ) {
                    DARKinputFolder = DFdefault.fsName.toString()
                       DFgroup.DFlabel.text = DARKinputFolder;

if(DFdefault){DARKinputFolder = DFdefault}; //if cancel is hit, this statement becomes false, and myFolder remains untouched.    
    //if it's true, then the selected folder from the holdFolder is transferred to myfolder.   

var FTlen, i;
FTlen = AllowedFileTypes.length;
for (i = 0; i < FTlen; i++){
var SEARCH_MASK = "*." + AllowedFileTypes[i]};
var DARKfileList = DARKinputFolder.getFiles(SEARCH_MASK) ;




 if (DARKfileList.length == 0)
{
    alert('The DARK FRAME folder you have chosen is either empty or does not contain image files of the correct type.\r\rPlease locate & select the correct image folder.','DARK FRAME folder error');
    DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
    DFdefault = '';
    DFgroup.DFlabelQ.text = '';
    }
else{                      
                       DFgroup.DFlabelQ.text = '[' + DARKfileList.length + ']';
                       var DFCurrFolder = (new File(DARKinputFolder)).fsName;
                       DFgroup.DFlabel.helpTip = 'Location of files to process:\r' + DFCurrFolder;
                       FolderChecker()
     }}
     } //opens the folder browse UI using you main folder.

BTNg.ExitBtn.onClick = function(){
dlgM.close();
} 

function FolderChecker(){
   if (DFdefault.exists){
    BTNg.ProcessBtn.enabled = true;
    }
}


BTNg.ProcessBtn.onClick = function(){

// THIS IS WHERE IMAGE PROCESSING STARTS -- REMOVED THIS HUGE SECTION FOR BREVITY

}


dlgM.show();

UPDATE 3 Final Issue

As per my comment about the need to check for illegal file types etc, here's what I have so far:

if (DARKfileList.length == 0)
{
    alert('empty folder!','DARK FRAME folder error');
    DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
    DFdefault = '';
    DFgroup.DFlabelQ.text = '';
    }

else if (DARKfileList.length > 1)
{
    alert('too many file types!','DARK FRAME folder error');
    DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
    DFdefault = '';
    DFgroup.DFlabelQ.text = '';
    }

else{                      
                       DFgroup.DFlabelQ.text = '[' + DARKfileList.length + ']';
                       var DFCurrFolder = (new File(DARKinputFolder)).fsName;
                       DFgroup.DFlabel.helpTip = 'Location of files to process:\r' + DFCurrFolder;
                       FolderChecker()
     }

Now this works perfectly if the chosen folder contains only one file & that file is not in the FILE_TYPE array as DARKfileList.length returns zero. As Required

Similarly if the folder contains more than one of any file no matter what type the check fails. As Required

However, if a chosen folder contains one allowed FILE_TYPE & one file type that is not in the FILE_TYPE array the check fails. Needs Sorting Out

I need something along the lines of:

if (DARKfileList != FILE_LIST)
{
        alert('Illegal file type!);
        DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
        DFdefault = '';
        DFgroup.DFlabelQ.text = '';
} 
4
  • can you boil your code down to something we could test? Commented Jun 20, 2016 at 12:41
  • @fabiantheblind added as update 2, regards.., Commented Jun 20, 2016 at 13:28
  • Forgot to add that the function that checks the folder for files of type as listed in the AllowedFileTypes variable need to show an alert if the indexed folder contains a mix of these types. The function should only continue to processing if the indexed folder contains only one type from the allowed types list. Hope that make it more clear as to the use Commented Jun 20, 2016 at 13:55
  • As per UPDATE 3 above, regards.., Commented Jun 21, 2016 at 17:16

1 Answer 1

2

The problem is that each iteration you overriding the resulting search mask.

You need to merge all the resulting into one array, and then to check if your array is empty:

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');

Alternative: getFiles can get regex as parameter, so if you familiar with regex you can simply use the following regex:

/.+\.(?:jpe?g|tiff?|orf)$/i

Regex explanation: match in the filename any character, then dot character, and then one of the extension option and then the i flag used to ignore case sensitive. In your example you get only files that the extension is in lowercase.

To add another extensions to the regex, just add pipe(|) after orf and then your extension. ex: to add pdf extension:

/.+\.(?:jpe?g|tiff?|orf|pdf)$/i

Usage of regex in your code:

DARKfileList = DARKinputFolder.getFiles(/.+\.(?:jpe?g|tiff?|orf|pdf)$/i);

You can also use array to store all of your extensions and use it like:

var regex = new RegExp('.+\.(?:' + FILE_TYPE.join('|')+ ')$','i');
DARKfileList = DARKinputFolder.getFiles(regex);
Sign up to request clarification or add additional context in comments.

2 Comments

Cheers, I'm away from my PC right now but I'll give your suggestions a try and report back
As per UPDATE 3 above, regards..,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.