在Extendscript

时间:2016-06-19 15:12:43

标签: extendscript

我正在尝试集成一个包含可以在我的Extendscript中使用的图像文件类型列表的数组,这就是我所拥有的:

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.

当用户点击&#39; DFBtn&#39;将出现一个打开文件夹对话框,他们可以选择包含图像的文件夹。

检索此文件夹路径及其中包含的图像数量,并将其作为文本标签输出到对话框中。

问题在于,通过测试并选择包含图像的文件夹并且该图像属于允许类型,该脚本直接进入“警告”状态。消息说明您选择的&#34;文件夹是空的.....&#34;等。

现在,如果我将代码的一小部分改为:

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

现在,代码不再直接进入“警报”状态。即使我故意选择一个空的文件夹。

更新

为了帮助找出正在发生的事情,我添加了一行额外的代码:

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

只需直接添加此行alert (SEARCH_MASK);,我就可以了解正在发生的事情。

是的FILE_TYPE数组正在迭代,但是SEARCH_MASK仅使用数组中的最后一个FILE_TYPE形成,所以在上面SEARCH_MASK的情况下始终创建为:

*.jpeg

因此,它只会在所选文件夹中查找该类型的文件。

这不是我想要实现的目标。

我想知道所选文件夹是否包含数组中列出的任何类型的任何文件。

例如,所选文件夹可以包含所有*.tif个文件,甚至包含每种文件类型中的一个。

如何实现我要求代码执行的操作?

问候...,

更新2 煮沸的脚本

根据要求,这是一个简单的脚本:

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

更新3 最终问题

根据我关于需要检查非法文件类型等的评论,这是我到目前为止所做的:

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()
     }

现在,如果所选文件夹只包含一个文件&amp;该文件不在FILE_TYPE数组中,因为DARKfileList.length返回零。 根据需要

同样,如果文件夹包含多个文件,则无论检查失败的类型如何。 根据需要

但是,如果所选文件夹中包含一个允许的FILE_TYPE&amp;一个不在FILE_TYPE数组中的文件类型,检查失败。 需要排序

我需要的是:

if (DARKfileList != FILE_LIST)
{
        alert('Illegal file type!);
        DFgroup.DFlabel.text = 'No DARK FRAMES folder has been selected...';
        DFdefault = '';
        DFgroup.DFlabelQ.text = '';
} 

1 个答案:

答案 0 :(得分:1)

问题在于每次迭代都会覆盖生成的搜索掩码。

您需要将所有结果合并到一个数组中,然后检查您的数组是否为空:

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

替代方法:getFiles可以将regex作为参数,因此如果您熟悉正则表达式,则只需使用以下正则表达式:

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

正则表达式解释:在文件名中匹配任何字符,然后匹配点字符,然后是扩展选项之一,然后使用i标志来忽略区分大小写。 在您的示例中,您只能获得扩展名为小写的文件。

要向正则表达式添加其他扩展程序,只需在|之后添加管道(orf),然后再添加您的扩展程序。例如:添加pdf分机:

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

在代码中使用正则表达式:

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

您还可以使用数组存储所有扩展程序并使用它:

var regex = new RegExp('.+\.(?:' + FILE_TYPE.join('|')+ ')$','i');
DARKfileList = DARKinputFolder.getFiles(regex);