Skip to main content
migueld83371718
Inspiring
May 26, 2019
Answered

CSInterface.evalScript callback scope

  • May 26, 2019
  • 1 reply
  • 3271 views

I'm trying to use the result of a call to the host application(After Effects), using CSInterface.evalScript, to modify the html:

var result = 'No result';

new CSInterface().evalScript('KT.getLayerNames("myComp")', function(res){

     result = res;

     alert(res);

})

alert(result)

The first alert throws the correct result, but the second keeps throwing 'No result'. I am confused, since normally this way of taking the results within a callback function works in javascript. I am missing something? Wich is the correct way to have the results available outside of the callback?

This topic has been closed for replies.
Correct answer Trevor:

The callback is asynchronous so the second  alert will not wait for the result from the call back.

You need to use a promise.

Here's a couple of examples

https://medium.com/adobetech/using-es6-promises-to-write-async-evalscript-calls-in-photoshop-2ce40f93bd8b

This one is a bit out dated https://www.davidebarranca.com/2014/12/html-panels-tips-15-asynchronous-vs-synchronous-evalscript/

1 reply

Trevor:
Trevor:Correct answer
Legend
May 26, 2019

The callback is asynchronous so the second  alert will not wait for the result from the call back.

You need to use a promise.

Here's a couple of examples

https://medium.com/adobetech/using-es6-promises-to-write-async-evalscript-calls-in-photoshop-2ce40f93bd8b

This one is a bit out dated https://www.davidebarranca.com/2014/12/html-panels-tips-15-asynchronous-vs-synchronous-evalscript/

JapinderSandhu
Inspiring
July 4, 2020

Hey Thanks for the great tutorial! Question — when I call runEvalScript I still get an undefined variable for example

 

	/* Helper function to create and return a promise object */
	runEvalScript(script:string) {

		console.log("SCRIPT INPUT",script)

		return new Promise(function(resolve, reject){

			// @ts-ignore
			new CSInterface().evalScript(script, resolve);

		});
	}

getActiveSequenceName (){

		var activeSequenceName: string

		// get current sequence name
		this.runEvalScript("$._PPP_.getActiveSequenceName()")
		.then(function(firstResult:string){

			activeSequenceName = firstResult

			console.log("ACTIVE SEQUENCE NAME", activeSequenceName)

			return activeSequenceName

			
		})
		.catch(function(error) {
			//handle any error that may occur in firstFunction()
			console.log("ASYNC ERROR",error)
		})

	}

var sequenceName = getActiveSequenceName()
console.log("SEQUENCE NAME",sequenceName)
//sequenceName = undefined

 

is there a way around this ?

 

With the console.logs I can see that the var sequenceName gets set before the actual runEvalScript completes, how do I get it to return the actual value and not undefined?

 

Is there a way to async wait for this specific variable?

anylizerexe2210
Participant
May 28, 2023

did you manage to do that? i'm having the same problem , tried with async and await not luck