Skip to main content
Shulipa Bernad
Inspiring
October 8, 2021
Answered

Error importing bridge file via listbox:

  • October 8, 2021
  • 1 reply
  • 449 views

Hello everyone!
My script gets and adds all the images selected in the bridge to the listbox, and it does something when I click the button, but if I remove some items from the listbox, it doesn't execute on the respective images in the list.
Ex: imag1, imag2 .... if I remove imag1 from the list, it would certainly run something on imag2, but it runs on imag1.
Could someone help me resolve this error?

var ImgConteudo=[]; 
dlg= new Window("dialog", "My Dialog", {x:0, y:0, width:230, height:360} );
var lbx = dlg.add("listbox", {x:15, y:10, width: 200, height: 200}, ImgConteudo, {multiselect: true}); 
b1 = dlg.add("button", {x:116, y:317, width: 99, height: 15},  "Remover" ); 
b2 = dlg.add("button", {x:15, y:317, width: 99, height: 15},  "Process" ); 

ImgConteudo = GetFilesFromBridge();
    for(i=0; i<ImgConteudo.length;i++){
    lbx.add("item", decodeURI(ImgConteudo[i].name));  
}

b1.onClick = function () {
     var sel = lbx.selection[0].index;
     for (var i = lbx.selection.length-1; i > -1; i--)
     lbx.remove (lbx.selection[i]);
     if (sel > lbx.items.length-1)
     lbx.selection = lbx.items.length-1;
     else lbx.selection = sel;
}

b2.onClick = function () {
  dlg.close(); 
 BridgeDocs()
}


dlg.center();
dlg.show();

function BridgeDocs(){  
     ImgConteudo = GetFilesFromBridge();
    for (var i = 0; i < lbx.items.length; i++){
    var d1 = new ActionDescriptor();
    d1.putPath(app.charIDToTypeID("null"), new File(ImgConteudo[i]));
    executeAction(app.charIDToTypeID("Opn "), d1, DialogModes.NO);
    alert(ImgConteudo[i]);
    }
}


function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
	var fileList;
		var bt = new BridgeTalk();
		bt.target = "bridge";
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
		bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
		bt.onError = function( inBT ) { fileList = new Array(); }
		bt.send(8); 
		bt.pump();
	if ( undefined == fileList ) fileList = new Array();
	return fileList; 
};
This topic has been closed for replies.
Correct answer jazz-y

 

var ImgConteudo=[]; 
dlg= new Window("dialog", "My Dialog", {x:0, y:0, width:230, height:360} );
var lbx = dlg.add("listbox", {x:15, y:10, width: 200, height: 200}, ImgConteudo, {multiselect: true}); 
b1 = dlg.add("button", {x:116, y:317, width: 99, height: 15},  "Remover" ); 
b2 = dlg.add("button", {x:15, y:317, width: 99, height: 15},  "Process" ); 

ImgConteudo = GetFilesFromBridge();
    for(i=0; i<ImgConteudo.length;i++){
    lbx.add("item", decodeURI(ImgConteudo[i].name));  
}

b1.onClick = function () {
     var sel = lbx.selection[0].index,
     tmp = ImgConteudo.slice();
     ImgConteudo = [];
     
    for (var i = 0; i < lbx.items.length; i++) 
    if (!lbx.items[i].selected) ImgConteudo.push(tmp[i])
    for (var i = lbx.selection.length-1; i > -1; i--) 
    lbx.remove (lbx.selection[i]);
         
     if (sel > lbx.items.length-1)
     lbx.selection = lbx.items.length-1;
     else lbx.selection = sel;
}

b2.onClick = function () {
  dlg.close(); 
  BridgeDocs()
}


dlg.center();
dlg.show();

function BridgeDocs(){  
    for (var i = 0; i < lbx.items.length; i++){
    var d1 = new ActionDescriptor();
    d1.putPath(app.charIDToTypeID("null"), new File(ImgConteudo[i]));
    executeAction(app.charIDToTypeID("Opn "), d1, DialogModes.NO);
    alert(ImgConteudo[i]);
    }
}


function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
	var fileList;
		var bt = new BridgeTalk();
		bt.target = "bridge";
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
		bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
		bt.onError = function( inBT ) { fileList = new Array(); }
		bt.send(8); 
		bt.pump();
	if ( undefined == fileList ) fileList = new Array();
	return fileList; 
};

 

1 reply

Legend
October 8, 2021

Names of the files in the listbox lbx and the paths in the ImgConteudo array are two different objects.

 

You only remove names from the listbox lbx, but not remove them from the ImgConteudo array. After removing from the listbox, accessing an element of the ImgConteudo array by index from lbx.items is incorrect (since these two arrays are no longer identical)

 

Also, when you call the BridgeDocs() function, you renew the list of files from the Bridge, this can lead to problems if the selection in Bridge has been changed.

Shulipa Bernad
Inspiring
October 8, 2021

@jazz-y Do you have any suggestions or would it never work?

 

jazz-yCorrect answer
Legend
October 8, 2021

 

var ImgConteudo=[]; 
dlg= new Window("dialog", "My Dialog", {x:0, y:0, width:230, height:360} );
var lbx = dlg.add("listbox", {x:15, y:10, width: 200, height: 200}, ImgConteudo, {multiselect: true}); 
b1 = dlg.add("button", {x:116, y:317, width: 99, height: 15},  "Remover" ); 
b2 = dlg.add("button", {x:15, y:317, width: 99, height: 15},  "Process" ); 

ImgConteudo = GetFilesFromBridge();
    for(i=0; i<ImgConteudo.length;i++){
    lbx.add("item", decodeURI(ImgConteudo[i].name));  
}

b1.onClick = function () {
     var sel = lbx.selection[0].index,
     tmp = ImgConteudo.slice();
     ImgConteudo = [];
     
    for (var i = 0; i < lbx.items.length; i++) 
    if (!lbx.items[i].selected) ImgConteudo.push(tmp[i])
    for (var i = lbx.selection.length-1; i > -1; i--) 
    lbx.remove (lbx.selection[i]);
         
     if (sel > lbx.items.length-1)
     lbx.selection = lbx.items.length-1;
     else lbx.selection = sel;
}

b2.onClick = function () {
  dlg.close(); 
  BridgeDocs()
}


dlg.center();
dlg.show();

function BridgeDocs(){  
    for (var i = 0; i < lbx.items.length; i++){
    var d1 = new ActionDescriptor();
    d1.putPath(app.charIDToTypeID("null"), new File(ImgConteudo[i]));
    executeAction(app.charIDToTypeID("Opn "), d1, DialogModes.NO);
    alert(ImgConteudo[i]);
    }
}


function GetFilesFromBridge() {
function script(){
var fL = app.document.selections;
var tF=[];
for(var a in fL){
    if(fL[a].type =='file'){
        tF.push(new File(encodeURI(fL[a].spec.fsName)));
        }
    }
return tF.toSource();
}
	var fileList;
		var bt = new BridgeTalk();
		bt.target = "bridge";
         bt.body = "var ftn = " + script.toSource() + "; ftn();";
		bt.onResult = function( inBT ) { fileList = eval( inBT.body ); }
		bt.onError = function( inBT ) { fileList = new Array(); }
		bt.send(8); 
		bt.pump();
	if ( undefined == fileList ) fileList = new Array();
	return fileList; 
};