Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
1

How to pass callback so AI will wait for PS to complete

Contributor ,
Apr 01, 2024 Apr 01, 2024

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?

TOPICS
Scripting
3.8K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Contributor , Apr 08, 2024 Apr 08, 2024

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!  

Translate
Adobe
Community Expert ,
Apr 01, 2024 Apr 01, 2024

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 01, 2024 Apr 01, 2024

@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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 02, 2024 Apr 02, 2024

//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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 02, 2024 Apr 02, 2024

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 08, 2024 Apr 08, 2024

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 08, 2024 Apr 08, 2024

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!  

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 08, 2024 Apr 08, 2024
LATEST

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines