BridgeTalk and Binary export failure

Discussion of Photoshop Scripting, Photoshop Actions and Photoshop Automation in General

Moderators: Tom, Kukurykus

undavide

BridgeTalk and Binary export failure

Post by undavide »

Hi,
I've noticed that BridgeTalk messages don't seem to like being binary exported.
The following script is just an over-simplification of an otherwise useful structure:

Code: Select allvar sendBridgeTalk = function(str) {
  var bridgeFunction, createBridgeTalk, isDone;
  createBridgeTalk = function(str) {
    var bt, returnValue;
    returnValue = false;
    bt = new BridgeTalk();
    bt.target = 'bridge';
    bt.timeout = 30;
    bt.body = "var bridgeFunction = " + (bridgeFunction.toSource()) + ";\nbridgeFunction(" + (str.toSource()) + ");";
    bt.onResult = function(btObj) {
      return returnValue = btObj.body;
    };
    bt.onError = function(btObj) {
      return $.writeln(btObj.body);
    };
    bt.send(60);
    return returnValue;
  };
  bridgeFunction = function(s) {
    $.writeln("In Bridge: " + s);
    return true;
  };
  isDone = createBridgeTalk(str);
  return isDone;
};

sendBridgeTalk('Greetings from Adobe Bridge');

The sendBridgeTalk function takes a string as the only parameter. It creates a BridgeTalk object and sends a message to Bridge, using an utility function - basically just a $.writeln(), passing to it the string.

As a result, Bridge writes in the console the message. Now, dumb as it may appear this example, it works.
Problem is that if I encrypt it to binary (ESTK: File - Export to Binary) and then I evaluate the long blob:

Code: Select alleval("@JSXBIN@ES@2.0@MyBbyBn0ACJInASzOjTjFjOjEiCjSjJjEjHjFiUjBjMjLByBNyBnAMIbyBn0AEJKn\
ASzQjDjSjFjBjUjFiCjSjJjEjHjFiUjBjMjLCBNyBnAMKbyBn0AKJMnASzLjSjFjUjVjSjOiWjBjMjV\
jFDBncfffJNnASzCjCjUEAEjzKiCjSjJjEjHjFiUjBjMjLFfntnffJOnABXzGjUjBjSjHjFjUGfVEfA\
neGjCjSjJjEjHjFfJPnABXzHjUjJjNjFjPjVjUHfVEfAndgefJQnABXzEjCjPjEjZIfVEfACzBhLJCJ\
CJCJnEXzIjUjPiTjPjVjSjDjFKfjzOjCjSjJjEjHjFiGjVjOjDjUjJjPjOLfnfeVjWjBjShAjCjSjJj\
EjHjFiGjVjOjDjUjJjPjOhAhdhAnnneRhbKjCjSjJjEjHjFiGjVjOjDjUjJjPjOhIEXKfVzDjTjUjSM\
fCnfnnnneChJhbnfJRnABXzIjPjOiSjFjTjVjMjUNfVEfANyBnAMRbyBn0ABZSnABjDfXIfVzFjCjUi\
PjCjKOfAnfABO40BhAB0AzAPCTnfJUnABXzHjPjOiFjSjSjPjSQfVEfANyBnAMUbyBn0ABZVnAEXzHj\
XjSjJjUjFjMjORfjzBhESfRBXIfVOfAffABO40BhAB0APCWnfJXnABXzJjPjOiUjJjNjFiPjVjUTfVE\
fANyBnAMXbyBn0ABZYnAEXRfjSfRBXIfVOfAffABO40BhAB0APCZnfJganAEXzEjTjFjOjEUfVEfARB\
FdhcffZgbnAVDfBADD4B0AiAM40BhAE40BiABCAPCgcnffJgdnASLANyBnAMgdbyBn0ACJgenAEXRfj\
SfRBCJnVzBjTVfAeLiJjOhAiCjSjJjEjHjFhahAnffZgfnAFctABV40BhAB0APChAnffJhBnASzGjJj\
TiEjPjOjFWCEVCfBRBVMfDffnffZhCnAVWfCAEC4B0AiAW4C0AiAM40BhAL40BiABDAPChDnffJhFnA\
EVBfyBRBFeNjQjBjTjTjFjEhAjTjUjSjJjOjHffABB40BiAABAPByB");

I got an error:

Code: Select allError in
Line 2: var bridgeFunction = (function anonymous() {  [compiled code] } );
Expected: ]

That is, it seems like the bridgeFunction I put in the BridgeTalk body (as .toSource()) is gone, or corrupt, or empty. The only workaround I've found (in the real script I'm working on) is to isolate all the delicate stuff and moving it outside the binary, as a plain code.
Which is a bit annoying for several reasons.
Any suggestion or thought?
Thank you!

Davide Barranca
http://www.davidebarranca.com
Mike Hale

BridgeTalk and Binary export failure

Post by Mike Hale »

I think the problem is when the script is encoded toSource() does not decode the function back into plain text. Instead it returns "[compiled code]". Something similar happens when you use toSource() on DOM methods. It returns '[native code]'.

I guess returning "[compiled code]" is a good thing most of the time or it would be too easy to break binary encoded scripts. Native objects like numbers,strings, etc seem to work correctly using toSource() when encoded.

Remember that BridgeTalk basically does an eval on the body string so the workaround is not to use toSource for the function. Just make the function definition part of the body string.
Code: Select all bt.body = "var bridgeFunction = function(s) { alert('In Bridge: ' + s);return true;};bridgeFunction(" + (str.toSource()) + ");";

Not as neat as using toSource() but it keeps all the code in one file and all encoded.
undavide

BridgeTalk and Binary export failure

Post by undavide »

Thank you Mike,
you've confirmed my experiments - I was working on the .toSource() step. In fact the only viable solutions are either to isolate the function above the binary eval, or stringify it in advance and pass the string directly!

Davide