Copy link to clipboard
Copied
Hi all! I've tried everything I can think of but unfortunately with no promises or awaits I'm kind of stuck.
My goal is to wait for PS to finish executing before AI continues. Here goes:
This portion of my script calls a method "runPScript" which executes in PS.
I'd like to wait for it to complete before AI takes off again. Alert runs immediately after PS launches.
for (var index = 0; index < templateSelected.length; index++) {
runPScript(templateSelected[index], smartObjTemplate, false);
}
alert('ran 5958')
I removed somet things to condense it, here is runPScript
function runPScript(template, smartObjTemplate, last) {
var bt = new BridgeTalk;
bt.target = "photoshop";
...
if(last) {
bt.onResult = function(resObj) {
alert("Illustrator is now running." + resObj.body);
}
bt.onReceived = function() {
return('completed');
}
}
bt.send(100);
}
I added the onReceived assuming that the script would wait for this command but obviously I'm wrong. Any idea how I can accomplish this?
1 Correct answer
Thank you all for the great pointers.
I ended up getting it to work by doing a return in my photoshop script. Once I did that it worked. That was my mistake, thank you again to all!
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi @OhmsG, I'm just learning this stuff too, so I might not be much help, but ... have you tried adding a return value to your bt.onResult function?
- Mark
Copy link to clipboard
Copied
@m1b Hi, yes I tried that but for some reason on.result doesn't trigger on mine. That alert doesn't execute only the onReceived triggers.
Copy link to clipboard
Copied
//your code structure should be:
for (var index = 0; index < templateSelected.length; index++) {
runPScript(templateSelected[index], smartObjTemplate, false);
}//code ends here!
function callBack() {
alert('ran 5958');
//..code to exec after ps call
}
...
bt.onReceived = callBack;
...
//another issue I see you will encounter, - you are calling runPsScript templateSelected.length times.
that means alert('ran 5958') will too be called templateSelected.length times.
you might calculate these in callback function to make sure to proceed only when all are executed, or better yet, register callback only on last item, since communication between apps is expensive.
edit - please ignore my second notice since i see you do check for last item to add listeners.
good luck!
Copy link to clipboard
Copied
Looks to me like your code isnt working as intended because you have your onresult and onreceived wrapped in an IF statement. then in the function calling this, you are setting the value of that IF to be false so the Bridgetalk object that you are sending to PS does not contain the onResult or the onReceived parts of the message.
any particular reason they are wrapped in the IF statement? does changing that value to true when passed in cause AI to wait?
Copy link to clipboard
Copied
Thanks for that, I missed it and removed it. But not it doesn't cause illustrator to wait. The bt.onReceived triggers right away. I need it to wait for photoshop to complete.
Copy link to clipboard
Copied
Thank you all for the great pointers.
I ended up getting it to work by doing a return in my photoshop script. Once I did that it worked. That was my mistake, thank you again to all!
Copy link to clipboard
Copied
Ah, yes I should have mentioned that. If there is no return value from the bt.body, then no callback get's triggered. Glad you sorted it out!

