Setting a Photoshop Script running from Bridge

General Discussion of Scripting for Adobe Bridge

Moderators: Tom, Kukurykus

v.bampton

Setting a Photoshop Script running from Bridge

Post by v.bampton »

I'm puzzled... and I'm sure there's a really simple answer.

I've been studying Andrew's Bridge scripts, and eventually realised I was making my idea far more complicated than necessary. I thought I needed to get Bridge to send an array of files to my photoshop script, until I realised that the photoshop script is already quite happily collecting the selected files, and all I actually need to do is set the script running. And there I get stuck!

Working on the basis of one of Andrew's scripts, I've so far got:
Code: Select alltApp = "photoshop-9.0"

function runScript(){
   if (app.document.selections.length  == 0 ) {
      alert( "No Files Selected" );
      return;
   } else {   
      BridgeTalk.bringToFront (tApp)
      var bt = new BridgeTalk ();
      bt.target = tApp;
      bt.body = "var f = new File('C:/Program Files/Adobe/Adobe Photoshop CS2/Presets/Scripts/DemoPSScript4.jsx');" +
      "if (f.open('r')) { scriptStr = f.read(); alert(scriptStr) } else { alert('Cannot open script') }" +
         "scriptStr"
      bt.send();   
   }   
}

runScript()

... and that's currently supposed to run this really simple little photoshop test script:

Code: Select allfileref = new File("C:/Documents and Settings/Vic/Desktop/IMG_8215.jpg");
open(fileref);
alert('file opened version 4');

I must be missing something though - for one thing, it takes forever to run it, if it does so at all. And it will run the alert within photoshop, but won't open the file (but it does when run directly from photoshop). Someone please put me out of my misery?! I've been puzzling over this for ages!

Thanks!!!
xbytor

Setting a Photoshop Script running from Bridge

Post by xbytor »

I personally favor getting the files from Bridge than send them to PS.
My library script for doing this reduces the application code down to:
Code: Select allvar files = XBridgeTalk.getBridgeSelection();

and that function is implemented like this:

Code: Select allXBridgeTalk.getBridgeSelection = function() {
  var brCode = ("function getBridgeFiles() {\n" +
                "  var v = app.document.selections;\n" +
                "  var files = [];\n" +
                "  for (var i = 0; i < v.length; i++) {\n" +
                "    var t = v;\n" +
                "    if (t != undefined) files = t.spec;\n" +
                "  }\n" +
                "  return files.toSource();\n" +
                "};\n" +
                "getBridgeFiles();\n");

  var br = XBridgeTalk.getAppSpecifier("bridge");
  if (!BridgeTalk.isRunning(br)) {
    return [];
  }
  var bt = new BridgeTalk();
  bt.target = br;
  bt.body = brCode;
  var str = bt.sendSync(10);
  return str ? eval(str) : [];
};


-X
v.bampton

Setting a Photoshop Script running from Bridge

Post by v.bampton »

Ok, thanks X, I'll look at that more carefully.

First things first though, how do I set a Photoshop script running from Bridge? Once I can get that working, I can look more carefully at the details.
v.bampton

Setting a Photoshop Script running from Bridge

Post by v.bampton »

Doh! Scratch that, I'd missed putting eval() around the scriptStr. Ok, on to your example now X!
v.bampton

Setting a Photoshop Script running from Bridge

Post by v.bampton »

Is there a way round this problem...?

I can get a photoshop script to launch from a bridge toolbar menu which I created, but only if the script does not include functions from another external script file. The script runs perfectly directly from photoshop, just not when called from Bridge.

It finds the script ok, and can read all the way through it using:

Code: Select allfunction runGalScript(){
   if (app.document.selections.length  == 0 ) {
      alert( "No Files Selected" );
      return;
   } else {   
      BridgeTalk.bringToFront (psApp)
      var bt = new BridgeTalk ();
      bt.target = psApp;
      bt.body = "var f = new File(app.path + '/Presets/Scripts/ShoppingCartGallery 460a.jsx');" +
      "if (f.open('r')) { scriptStr = f.read(); eval(scriptStr);} else { alert('Cannot open script') }"
      bt.send();   
   }   
}

but then it stalls when in that 460a file it comes across the line:
Code: Select all//@include "ShoppingCartHTML 460b.jsx"

Is there a solution to this one, or will I need to combine the whole script into one rather than using external files?

Thanks guys - I'm learning sooooo much from you!!!
Andrew

Setting a Photoshop Script running from Bridge

Post by Andrew »

I have never tried includes in a BT launched script. You could try reading and evaling the target instead, that should work.

Andrew
v.bampton

Setting a Photoshop Script running from Bridge

Post by v.bampton »

Andrew wrote:I have never tried includes in a BT launched script. You could try reading and evaling the target instead, that should work.

Andrew

Reading and evaling the target? Ok, a clue as to how I'd do that....?
Mike Hale

Setting a Photoshop Script running from Bridge

Post by Mike Hale »

Include should work. At least it does for me but I always use full paths. Try adding either the full path to the include or use //@includepath

Mike
Andrew

Setting a Photoshop Script running from Bridge

Post by Andrew »

Mike is presumably right about the includes but for the eval method:

Code: Select alltest();

function test() {
   var scriptStr = readFile(File('/c/testscripts/evaltest.js'));
   eval(scriptStr);
}

function readFile(f,bin) {
   var s;
   if (!f.exists){alert('Function readFile File does not exist\n' + f.fsName);return -1;}
   if (bin == true)f.encoding  = 'BINARY';
   if (!f.open('r')) {alert('Function readFile Failed to Open File\n' + f.fsName);return -1;}
   s = f.read();
   f.close();
   return s;
}

evaltest.js is any javascript code you want. You do need to be mindful of scoping issues with this approach.

Andrew
Mike Hale

Setting a Photoshop Script running from Bridge

Post by Mike Hale »

I think what is happening is that that when you normally run a script Photoshop knows the folder the script is in. When there is an include that doesn't have the full path, Photoshop looks in that folder for the file. Because the script here is being run by eval, Photoshop doesn't know where to look and can't find the included file

It never hurts to use the full path. The include directive will need to be edited in any case. Doing an eval will run the file you wanted included. However, you will still get the error when you eval the original script because Photoshop still can't find the file. So if you want to go that way you will need to remove the include statement.

I would think that any scoping issues would be the same either way.

Mike