Skip navigation
Currently Being Moderated

How to get result from BridgeTalk message?

Sep 24, 2008 10:35 AM

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; = "photoshop";
   bt.body = "app.colorSettings;"
   bt.onResult = function(resObj) {
      var myResult1 = resObj.body;
      $.writeln("myResult1 = " + myResult1);
      return myResult1;

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.
  • Currently Being Moderated
    Sep 25, 2008 9:24 AM   in reply to Kasyan Servetsky
    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(); = "photoshop";
    bt.body = "app.colorSettings;"
    bt.onResult = function( msg ) {
    var myResult = msg.body;
    $.writeln( "BridgeTalk result = " + myResult );
    doSomethingNow( myResult );

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


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

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


    Mark as:
  • Currently Being Moderated
    Nov 3, 2011 7:43 PM   in reply to Kasyan Servetsky

    Hi Bob & Kasyan,


    Currently i am working with InDesign CS3. I need to check whether the Object layered images preseneted in my InDesign document, if it is found i need to open the particular image (.ai or .psd) in the (Illustrator or Photoshop) applications and do some layer manipulation in it and do saveAs with different name. Finally i need to relink the image in InDesign.


    I have written the script for InDesign, Illustrator & Photoshop seperately for the above stuff. The only thing i need to do is combine these scripts and make them intermediate. Finally i have to run this script from InDesign Scripts palette.


    In a script, targetting the different applications is not work out i think so. Hence i tried with Bridge Talk() but facing difficulties on execution from ESTK.


    Below is my code:


    #target InDesign-5.0

    var doc = app.activeDocument;

    var docLinks = doc.links;

    var OLImage = [];

    var OLayer = [];

    var myCheck = 0;


    for (e=0; e<docLinks.length; e++){

        var myLink = docLinks[e];

        var myGLayer = myLink.parent.graphicLayerOptions.graphicLayers;

        if (myGLayer.length > 1){

            for (g=0; g<myGLayer.length; g++){

                if (myGLayer[g].currentVisibility != myGLayer[g].originalVisibility){




                    myCheck = 1;



        if (myCheck == 1){

            var myUpdatedImage = setLayerVisibility(myLink.filePath, OLImage);

            alert (myUpdatedImage);







    function setLayerVisibility(myFile, ObjLayers){


        if (myFile.toString().match(/\.ai$/gi) != null){


    //~         #target Illustrator-13.0


            var doc =;

            var docPath = doc.path;


            for (o=0; o<ObjLayers.length; o++){

                if (ObjLayers[o][1])

                    doc.layers.getByName(ObjLayers[o][0]).visible = true;





            var saveOptions = new IllustratorSaveOptions();

            var aiCS4Doc = new File(docPath + "/" +".ai", ""));

            saveOptions.compatibility = Compatibility.ILLUSTRATOR13;

            saveOptions.flattenOutput = OutputFlattening.PRESERVEAPPEARANCE;

            doc.saveAs( aiCS4Doc, saveOptions );

            return aiCS4Doc;



        else if (myFile.toString().match(/\.psd$/gi) != null){


    //~         #target Photoshop


            var doc =;

            var docLayers = doc.layers;

            var docPath = doc.fullName.toString().replace(/\.psd$/gi, "-b.psd");


            for (p=0; p<ObjLayers.length; p++){

                if (ObjLayers[p][1])

                    doc.layers.getByName(ObjLayers[p][0]).visible = true;





            doc.saveAs (File(docPath));

            return docPath;





    Can you please guide me in this regard.



    Thanks in advance


    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)