Photoshop脚本;对话框打开多个文档,但得到" undefined"

时间:2017-10-12 14:03:58

标签: javascript photoshop photoshop-script

我有一个对话框,允许用户浏览一个PSD文件,然后浏览多个 TIF文件。浏览多个TIF文件时,edittext框中的文本将显示为undefined。如果我只是删除了选择多个文件的能力,它就可以了。

var dlg = new Window('dialog', 'PSD Creator', [100, 100, 500, 550] );  

dlg.pnl_browse = dlg.add('panel', [10, 10, 390, 150], 'Browse');
    dlg.pnl_browse.txt_staticPSD = dlg.pnl_browse.add('statictext' , [15, 10, 375, 30],'Select the project images folder:');
    dlg.pnl_browse.btn_browsePSD = dlg.pnl_browse.add ('button', [15, 35, 60, 60], '...');
    dlg.pnl_browse.txt_editPSD = dlg.pnl_browse.add('edittext' , [65, 35, 365, 55],'<Select the project images folder>');
    dlg.pnl_browse.txt_staticTIFF = dlg.pnl_browse.add('statictext' , [15, 70, 375, 90],'Select the folder where you TIFF images are:');
    dlg.pnl_browse.btn_browseTIFF = dlg.pnl_browse.add ('button', [15, 95, 60, 120], '...');
    dlg.pnl_browse.txt_editTIFF = dlg.pnl_browse.add('edittext' , [65, 95, 365, 120],'<Select the folder where you TIFF images are>');    

dlg.btn_ok = dlg.add ('button', [70,400,190,430], 'ok');
dlg.btn_cancel = dlg.add ('button', [210,400,320,430], 'cancel');


dlg.pnl_browse.btn_browsePSD.onClick = function ()  {   
    selectFilePSD = File.openDialog("Please select your template file.","*.psd");   
        if(selectFilePSD != null) dlg.pnl_browse.txt_editPSD.text =  decodeURI(selectFilePSD.fsName); 
}


dlg.pnl_browse.btn_browseTIFF.onClick = function ()  {   
    selectFileTIFF = File.openDialog("Please select your tiff images.","*.TIF; *TFF", true);  
        if(selectFileTIFF != null) dlg.pnl_browse.txt_editTIFF.text =  decodeURI(selectFileTIFF.fsName); 
 }       

dlg.btn_ok.onClick = function () {

        dlg.close()
        open (selectFilePSD);
        open (selectFileTIFF);


}

dlg.center(); 

dlg.show();

1 个答案:

答案 0 :(得分:0)

看起来问题是你需要一个数组来保存用户选择的文件。有了这些知识,其余的代码就会相应地重新编写。添加了不同的情况以对不同的可能性做出反应(无论数组是否包含多个值),然后只需用for循环打开数组中的所有文件!希望这会有所帮助:

var dlg = new Window('dialog', 'PSD Creator', [100, 100, 500, 550] );
var selectFileTIFF = [];

dlg.pnl_browse = dlg.add('panel', [10, 10, 390, 150], 'Browse');
    dlg.pnl_browse.txt_staticPSD = dlg.pnl_browse.add('statictext' , [15, 10, 375, 30],'Select the project images folder:');
    dlg.pnl_browse.btn_browsePSD = dlg.pnl_browse.add ('button', [15, 35, 60, 60], '...');
    dlg.pnl_browse.txt_editPSD = dlg.pnl_browse.add('edittext' , [65, 35, 365, 55],'<Select the project images folder>');
    dlg.pnl_browse.txt_staticTIFF = dlg.pnl_browse.add('statictext' , [15, 70, 375, 90],'Select the folder where you TIFF images are:');
    dlg.pnl_browse.btn_browseTIFF = dlg.pnl_browse.add ('button', [15, 95, 60, 120], '...');
    dlg.pnl_browse.txt_editTIFF = dlg.pnl_browse.add('edittext' , [65, 95, 365, 120],'<Select the folder where you TIFF images are>');    

dlg.btn_ok = dlg.add ('button', [70,400,190,430], 'ok');
dlg.btn_cancel = dlg.add ('button', [210,400,320,430], 'cancel');


 dlg.pnl_browse.btn_browsePSD.onClick = function ()  {   
selectFilePSD = File.openDialog("Please select your template file.","*.psd");   
        if(selectFilePSD != null) dlg.pnl_browse.txt_editPSD.text = decodeURI(selectFilePSD.fsName); 
}


dlg.pnl_browse.btn_browseTIFF.onClick = function ()  {   
selectFileTIFF = File.openDialog("Please select your tiff images.","*.TIF; *TFF", true); 

    //selectFileTIFF is an array  thus you must loop through each selected file
    //you will need a String variable to appened the name of each file for your ScriptUI panel 
    if( selectFileTIFF.length != 0 ) {
        if( selectFileTIFF.length > 1 ) {
            dlg.pnl_browse.txt_editTIFF.text = selectFileTIFF.length + " items selected";
        } else {
            dlg.pnl_browse.txt_editTIFF.text = decodeURI( selectFileTIFF[0].fsName );
        } //end else
    } //end if
 } //end func      

dlg.btn_ok.onClick = function () {

        dlg.close()
        open (selectFilePSD);

        //must open each file is selectFileTIFF array
        for ( i = 0; i < selectFileTIFF.length; i++ ) {
        open (selectFileTIFF[i]);
        }


}

dlg.center(); 

dlg.show();