looping through thumbnails stops unexpectedly after 30-60

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

jamesrmac

looping through thumbnails stops unexpectedly after 30-60

Post by jamesrmac »

I wrote what i thought was a fairly simple script to add a right click menu to loop through selected thumbnails in bridge and open each one in photoshop and run an action. It works, but will often stop before it finishes all the thumbnails. It gives no errors, and I have been tearing my hair out trying to figure out what I could be missing. Sometimes it will finish up to 180 and other times it will only finish 30-60 (this is on the same pc). I have pulled apart the main function for direct testing in ESTK with no right click menu
Code: Select allvar filesflatten = [];
var f =  app.document.selections;
for (var i =0; f.length > i;  i++) {
    if (f.spec instanceof File){ filesflatten.push(f.spec);   }
                  }
-for (var i =0; filesflatten.length > i;  i++) {                                    //for each file selected
                    scr = "app.open(" + filesflatten.toSource () + ");";                //open the file in photoshop
                    scr +="doAction(\"Universal Flattener\", \"Flatten\");";      //run the action
                    var bt = new BridgeTalk();
                    bt.target = "photoshop";
                    bt.body = scr;
                    bt.onError = function(errybody){alert(errybody.body); }
                    bt.send();                                                                       //send the message to photoshop
                    }
I am hoping someone with more experience will look at this and point out the mistake im making, or what could make bridge bail on a script like that without erroring on me. Ive been lurking around here for months now learning lots of stuff, props to everyone that makes this site happen.
Paul MR

looping through thumbnails stops unexpectedly after 30-60

Post by Paul MR »

You are sending sequential calls to Photoshop and not checking if Photoshop has done what it needs to do.
It would be better just to do one call sending the complete file list IE:-

Code: Select all#target bridge
if( BridgeTalk.appName == "bridge" ) {
  flattenFiles = new MenuElement("command", "Flatten Files", "at the end of Thumbnail");
  flattenFiles.onSelect = function () {
var selectedFiles=[];
for(var a = 0 ; a<app.document.selections.length;a++){
    selectedFiles.push(app.document.selections[a].spec);
    }
function script(fileList){
    for(var a in fileList){
        open(File(fileList[a]));
        doAction("Universal Flattener", "Flatten");
        /*Save and close if the action does not do this.*/
        }
}

var bt = new BridgeTalk();
bt.target = "photoshop";
bt.body = "var ftn = " + script.toSource() + "; ftn(" + selectedFiles.toSource() +");";
bt.send(4);
    }
};

I hope your action saves and closes each document.
jamesrmac

looping through thumbnails stops unexpectedly after 30-60

Post by jamesrmac »

Thanks Paul! Not only does that make good sense, you example showed me how to pass a whole function back and forth with bridgetalk which will be very useful.