Skip to main content
Inspiring
March 26, 2024
Answered

Wait for BT to complete before illustrator proceeds?

  • March 26, 2024
  • 2 replies
  • 721 views

I currently have a scrippt running in AI that sends a command to PS and it executes.  The issue I"m having is that i need PS to complete before AI proceeds.

 

In my jSX file I have:

 

 

  for (var index = 0; index < templateSelected.length; index++) {
      runPScript(templateSelected[index], smartObjTemplate);
  }

 

This opens photoshop to execute a script:

 

function runPScript(template, smartObjTemplate) {
var bt = new BridgeTalk;
bt.target = "photoshop";

var script = '\#include \"/Library/Application\ Support/Adobe/CEP/extensions/Yoo_11/jsx/smartObjPrep.jsx\";\n';
script += 'main("' + template + 'separator' + smartObjTemplate + '")';

bt.body = script;
bt.send(10);

return("Completed");
}

 

The issue I'm having is that Illustrator continues while the PS command runs.  That wouldn't be an issue except that this tiem it is.  Since I need it to wait so I can execute something else.

I assumed the for loop was synchronous in JSX and would prevent it from running until completed.  Any idea how I can accomplish this?  

This topic has been closed for replies.
Correct answer RobOctopus

I have been met with inconsistent results of success when trying to debug bridgetalk functions. while testing some things out I wrote a small script to create a QR code in Illustrator by Bridgetalking to Indesign to use the built in QR functions there, and then copy paste it into Illustrator. Overall here are my findings.

 

  1. you will need a timeout value in the send statement. this number is the number of seconds it will wait before resuming functions in the host application. as long as the script in the target can be handled in this time frame... everything works fine.
  2. you will need onResult and possibly OnReceived
  3. I found using the OnReceived to activate the application has been helpful. Some of the apps dont like to operate while not in the activate state

 

Someone smarter than me may be able to offer more insight, but i would try a recevied message that PS will do and also an onresult message that will be pushed back to illustrator, which can be a nothing statement and maybe push the timeout to be a little longer. Happy hunting, bridgetalk is a pain.

 

helpful resource 

https://extendscript.docsforadobe.dev/interapplication-communication/bridgetalk-message-object.html

 

var PromptRes = prompt("Website:","www.Google.com")

    var BT = new BridgeTalk();
    var appspecifier = "indesign";
    if(!BridgeTalk.isRunning(appspecifier)){
        BridgeTalk.launch(appspecifier);
    }

    //var ScriptFileMessage = 'var myDocument = app.documents.add();\rvar QRTangle = myDocument.rectangles.add({geometricBounds:[0.5,0.5,2,2]});\rQRTangle.createHyperlinkQRCode("' + PromptRes + '");\rQRTangle.select();\rapp.copy();\rmyDocument.close(SaveOptions.NO)'
    var ScriptFileMessage = 'var myDocument = app.documents.add();'
    ScriptFileMessage += 'var QRTangle = myDocument.rectangles.add({geometricBounds:[0.5,0.5,2,2]});'
    ScriptFileMessage += 'QRTangle.createHyperlinkQRCode("' + PromptRes + '");'
    ScriptFileMessage += 'QRTangle.select();'
    ScriptFileMessage += 'app.copy();'
    //ScriptFileMessage += '$.sleep(6000);'
    /* testing that it will wait. as long as the function handles before the timeout on the send it should be fine */
    ScriptFileMessage += 'app.activeDocument.close(SaveOptions.NO);'
    BT.target = appspecifier;
    BT.body = ScriptFileMessage;
    /*alert(BT.body)*/
    BT.onResult = function(){
        app.paste();
    }
    BT.onReceived = function(){
        /*forcing the trget application to become frontmost and active. this is the most troublesome part and sometimes likes to work, sometimes it doesnt*/
        var IDActivate = File(BridgeTalk.getAppPath(appspecifier))
        IDActivate.execute();
    }
    BT.send(10);

 

2 replies

RobOctopus
RobOctopusCorrect answer
Inspiring
March 27, 2024

I have been met with inconsistent results of success when trying to debug bridgetalk functions. while testing some things out I wrote a small script to create a QR code in Illustrator by Bridgetalking to Indesign to use the built in QR functions there, and then copy paste it into Illustrator. Overall here are my findings.

 

  1. you will need a timeout value in the send statement. this number is the number of seconds it will wait before resuming functions in the host application. as long as the script in the target can be handled in this time frame... everything works fine.
  2. you will need onResult and possibly OnReceived
  3. I found using the OnReceived to activate the application has been helpful. Some of the apps dont like to operate while not in the activate state

 

Someone smarter than me may be able to offer more insight, but i would try a recevied message that PS will do and also an onresult message that will be pushed back to illustrator, which can be a nothing statement and maybe push the timeout to be a little longer. Happy hunting, bridgetalk is a pain.

 

helpful resource 

https://extendscript.docsforadobe.dev/interapplication-communication/bridgetalk-message-object.html

 

var PromptRes = prompt("Website:","www.Google.com")

    var BT = new BridgeTalk();
    var appspecifier = "indesign";
    if(!BridgeTalk.isRunning(appspecifier)){
        BridgeTalk.launch(appspecifier);
    }

    //var ScriptFileMessage = 'var myDocument = app.documents.add();\rvar QRTangle = myDocument.rectangles.add({geometricBounds:[0.5,0.5,2,2]});\rQRTangle.createHyperlinkQRCode("' + PromptRes + '");\rQRTangle.select();\rapp.copy();\rmyDocument.close(SaveOptions.NO)'
    var ScriptFileMessage = 'var myDocument = app.documents.add();'
    ScriptFileMessage += 'var QRTangle = myDocument.rectangles.add({geometricBounds:[0.5,0.5,2,2]});'
    ScriptFileMessage += 'QRTangle.createHyperlinkQRCode("' + PromptRes + '");'
    ScriptFileMessage += 'QRTangle.select();'
    ScriptFileMessage += 'app.copy();'
    //ScriptFileMessage += '$.sleep(6000);'
    /* testing that it will wait. as long as the function handles before the timeout on the send it should be fine */
    ScriptFileMessage += 'app.activeDocument.close(SaveOptions.NO);'
    BT.target = appspecifier;
    BT.body = ScriptFileMessage;
    /*alert(BT.body)*/
    BT.onResult = function(){
        app.paste();
    }
    BT.onReceived = function(){
        /*forcing the trget application to become frontmost and active. this is the most troublesome part and sometimes likes to work, sometimes it doesnt*/
        var IDActivate = File(BridgeTalk.getAppPath(appspecifier))
        IDActivate.execute();
    }
    BT.send(10);

 

OhmsGAuthor
Inspiring
March 27, 2024

The onReceived method worked! THANK YOU!

m1b
Community Expert
Community Expert
March 27, 2024

Hi @OhmsG, is it possible to use the onResult method of the bridge talk message? I learned a bit about it recently at this thread in the Indesign forum. You can see some scripts there that show using it. The main considerations are (a) assign a callback function to the bt.onResult and (b) IMPORTANT: your PS script *must return a value* or the callback won't execute. and (c) the object passed to the callback is another BridgeTalk message and the returned object is the `body` property.

 

(I'm assuming that BridgeTalk is the same in Illustrator as in Indesign.)

- Mark

OhmsGAuthor
Inspiring
March 27, 2024

Thank you, that thread was very helpful.  I set the code like this, but unfortuntately I don't get an alert in illustrator. 😞

function runPScript(template, smartObjTemplate) {
    var bt = new BridgeTalk;
    bt.target = "photoshop";
    
    var script = '\#include \"/Library/Application\ Support/Adobe/CEP/extensions/Yoo_11/jsx/smartObjPrep.jsx\";\n';
    script += 'main("' + template + 'separator' + smartObjTemplate + '")\n';
    script += 'return "completed ps";'

    bt.body = script;
    bt.onResult = function(resObj) {
        alert("Illustrator is now running." + resObj.body);
    }

    bt.send(100);
}

My expected result was that the alert would appear in illustrator but that is not the case.  Any idea what I'm missing?