Skip to main content
Kasyan Servetsky
Legend
September 24, 2008
Question

How to get result from BridgeTalk message?

  • September 24, 2008
  • 2 replies
  • 14900 views
Dear all,

I want to add a new function to my preflight script which should check color settings chosen in Photoshop. My major problem is that I cant get result outside of bt.onResult function.
Here is where I got so far:


#target indesign-5.0
#targetengine "session"
var myResult2 = infoFromPS();
$.writeln("myResult2 = " + myResult2);

function infoFromPS() {
var bt = new BridgeTalk;
bt.target = "photoshop";
bt.body = "app.colorSettings;"
bt.onResult = function(resObj) {
var myResult1 = resObj.body;
$.writeln("myResult1 = " + myResult1);
return myResult1;
}
bt.send();
}

It writes myResult1 variable to console as expected, but myResult2 is undefined. Why?

Another question:
Ive been experimenting with BridgeTalk for some time and found out a strange phenomenon. When I send a BridgeTalk message, lets say, from InDesign to Photoshop in main target engine this script doesnt work, but when in session or a custom one it works. Why so?

I want to explore the possibilities of interapplication communication for a long time. But I cant get reliable information on this topic. Ive read Interapplication Communication with Scripts chapter in JavaScript Tools Guide several times but I couldnt get the example scripts (in Communicating Through Messages section) working.
Ive found a few examples in Adobe Bridge CS3 SDK: SnpSendArray.jsx, SnpSendMessageToInDesign.jsx etc. They work like a charm but their code is hard for me to understand Im not a programmer by background.

So, I have two favors to ask of you:
1st Could someone give me complete and working examples, similar to given in Communicating Through Messages section?
2nd Could somebody explain to me, in simple terms , some example from Bridge CS3 SDK?

Thanks in advance.
Kasyan
    This topic has been closed for replies.

    2 replies

    Kasyan Servetsky
    Legend
    September 25, 2008
    Hi Bob,

    Thank you so much for the quick reply and intelligible explanation. You opened my eyes to this problem. I reached a deadlock and you showed me the way out of it. I wouldn't be able to solve this thing without your help. Now it works at last!
    Thank you again, Bob.

    Kasyan

    #target indesign-5.0
    
    #targetengine "session"

    function infoFromPS() {
    var bt = new BridgeTalk;
    bt.target = "photoshop";
    bt.body = "app.colorSettings;"
    bt.onResult = function(resObj) {
    var myResult = resObj.body;
    $.writeln( "BridgeTalk result = " + myResult );
    doSomethingNow( myResult );
    }
    bt.send();
    }

    function doSomethingNow( result ) {
    if (result != "ISO Web Coated") {
    alert( "Color Settings in Photoshop are set to \"" + result + "\"");
    }
    }

    infoFromPS();
    bagonterman
    Inspiring
    January 27, 2016

    Can you call an error from the body of the string message your sending in the BridgeTalk?

    I need to make an error when my script errors.

    Thanks BG

    Kasyan Servetsky
    Legend
    February 2, 2016

    Sorry about that Kasyan I wanted to but I could not find the tool for it in the menu. I just looked at the javascript guide that you showed.

    I was thinking maybe having the function return a header bt.headers ["Error-Code"] = -29;


    Theoretically, you can set your own error code number (it's a read/write property), but in practice I've never done this. As far as I understand, this gives the user idea what sort of problem occurred in the sender application. I don't see the reason to change it.

    September 25, 2008
    Your questions tell me that you don't really understand the asynchronous behavior or BridgeTalk.

    When you call bt.send() to send your BridgeTalk message, your script doesn't stop. I continues to execute until completion. Your onResult handler function is called when a result is received from the target application.

    Without a persistent session, InDesign (and PS) start a scripting engine for your script. When your script completes, the scripting engine is killed off.

    If you send a BridgeTalk message in such a script, when the result comes back to InDesign, the reference to your script and its scripting engine no longer exists. That's why you MUST use a persistent session in InDesign if you are sending a BridgeTalk message and expecting a response.

    In the code you provided, think of the onResult handler as a separate function to be executed at some future time.

    Your function infoFromPS() does not return anything, which is why the first writeln returns "undefined".

    Your onResult function will execute when a result is received; but, that will be at some future time, long after your infoFromPS() function has already returned.

    The execution works like so:

    var myResult2 = infoFromPS();
    - this causes the infoFromPS() function to execute...

    infofromPS() creates a BT message, sets the properties including the onResult handler, and calls "bt.send()". It then completes, and returns nothing (undefined).

    The next line to execute (because infoFromPS() has compelted) is:

    $.writeln("myResult2 = " + myResult2);

    And since infoFromPS() returned noting, you get

    myResult = undefined

    in the ESTK.

    Then, at some time (depends on how long the script takes to execute in Photoshop - could be hours), a response message is received from Photoshop. At that time, your onResult handler executes:

    myResult1 is set to the response message body, the $.writeln writes the correct stuff into the ESTK console, and the onResult function returns myResult1 (which does nothing). You can't "return" something from an onResult handler.

    The correct way to use BridgeTalk is to write your script so that it sends the message, and then to write the onResult handler so it does what you need with the returning message.

    #target indesign;
    #targetengine "session"

    function infoFromPS() {
    var bt = new BridgeTalk();
    bt.target = "photoshop";
    bt.body = "app.colorSettings;"
    bt.onResult = function( msg ) {
    var myResult = msg.body;
    $.writeln( "BridgeTalk result = " + myResult );
    doSomethingNow( myResult );
    }
    bt.send();
    }

    doSomethingNow = function( result ) {
    $.writeln( "Now I am doing something with " + result );
    }

    infoFromPS();

    This *should* (I haven't tested it) write into the ESTK console:

    "BridgeTalk result..."
    "Now I am doing something with..."

    Regards

    Bob