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

i am not able to call more than one function which returns a value using csInterface in js

Community Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

i have already called a function in js to fetch a value from jsx using csInterface but i am not able to call another function which returns something using csInterface.please suggest what should i do

TOPICS
How to , Scripting

Views

405

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

This does not give you anything to help you. Tell us what do you call? How do you call? Do you mean you are just able to call only a particular method using csinterface, or that you are only able to make just one method call in a session?

Paste code snippets, explain it better so that we can understand the problem.

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

in this snippet i am not able to get returned value from check fn. but i have got returned value from gettext .why is it so when i have used the same process.check() function returns 1 or 0  from jsx code.actually the other fns used do not retiurn any value so they work well but as i have a secong fn i.e check which returns a value it does not work
 

 

 

 

function translate()
{
	csInterface.evalScript("getText()", function(res)
	{
		var t='?translator_id='+document.getElementById("f").value;
		//alert(t);
		var v='&target='+document.getElementById("g").value;
		//alert(v);
		var url='http://localhost:3000/translate'+t+v;
		//var url = 'http://localhost:5000/name?pd=1&target=fr' ;
		var para = res.split("\r");
		for(var i=para.length;i>=0;i--)
		{
			if(para[i]==undefined)
				continue;
			//csInterface.evalScript("alert('" + para[i] + "')");
			var da = JSON.stringify({ "data" : para[i] });
			var xhr = new XMLHttpRequest();
			xhr.open('POST', url, false);
			xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
			xhr.onload = function (e)
			{
				if (this.status == 200 || this.status == 304)
				{
					var lan = document.getElementById("g").value;
					if(lan=="HI" || lan=="BN" || lan=="AR" || lan=="ZH" || lan=="GU" || lan=="KO" ||lan=="JA" || lan=="ML"|| lan=="MI" || lan=="NE" || lan=="MN" || lan=="PA" || lan=="MR" || lan=="TA" || lan=="TO" ||lan=="BO" || lan=="UR"){
						csInterface.evalScript("printTranslation('"+ this.responseText + "','" + i + "')");
					}
					else
					{
						csInterface.evalScript("translation('"+ this.responseText + "','" + i + "')");
					}
					
				}
				else
					csInterface.evalScript("alert('Server error: " + this.status + "')");
			};
			xhr.send(da);
		}
		csInterface.evalScript("alert('Translation Complete!')");
	});
	setTimeout(function(){
            			csInterface.evalScript("check()", function(data)
            			{
							if(data==1)
							{
								csInterface.evalScript("alert('" + this.data + "')");
							}
							else{
								csInterface.evalScript("alert('" + this.data + "')");
								csInterface.evalScript("alert('nothing')");
							}

            			});
					  }.bind(this), 1000);
	
}

 

 

 

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

the given below is my code .actualyy earlier i had submitted my whole actual code but it was deleted i dont know why so this time i have removed the contents sof gettext evalscript.my p0roblem is that when i am using csinterface.evalscript to fetch a return value from jsx only one fn is working properly,like gettext is returning value but check function is working .i get either blank or undefined.i have also used settimeout and bind but no results.can u suggest me solution 
function translate()
{
    csInterface.evalScript("getText()"function(res)
    {
       //some code snippet which is working fine
        }
        csInterface.evalScript("alert('Translation Complete!')");
        setTimeout(function(){
            csInterface.evalScript("check()"function(data)
            {
                if(data==1)
                {
                    csInterface.evalScript("alert('" + this.data + "')");
                }
                else{
                    csInterface.evalScript("alert('" + this.data + "')");
                    csInterface.evalScript("alert('nothing')");
                }

            });
          }.bind(this), 1000);
    });
    
    
}

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

Ok so you mean to say check method is called but the return value is not what you expect and its blank or undefined, right? If so then we will need to have a look at your jsx code for check method, that should be the culpritm csinterface is working fine on sending your call to jsx engine. Paste the snippet for check method, i will have a look

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

function check()
{
    return "1";
}

there is nothing in check method just for now i have simple 1 still its not returning any value.

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

If this is so simple then i think i would have to look at your whole extension. Please upload a stripped version of the whole extension folder on Dropbox or other cloud service and post the public url here. The code you posted previously looks strange to me, you have added the callback function to the csinterface call, then the other code seems to be out of the method, it seems incorrect to me. However if i have the whole extension code i can look into it. Do mind include only the specific code needed and give instructions on how to reproduce the issue

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

actually initially code was

function check()
{
    var mydoc = app.documents.item(0);
        var item = app.selection;
        var myframe = item[0];
        var paras = myframe.parentStory.contents;
        return paras;
}
which wasnt working so replaced it by return 1 simply to check if it is returning anything.
thank you for your help .should i add my whole code in dropbox or just paste the particular function

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

Upload the whole extension folder to dropbox and give the public access url here.

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

Works fine for me, See the recording below

https://www.dropbox.com/s/hfj6owfv4r3crdk/screen%20recording%202020-06-16%20at%208.12.27%20am.mov?dl...

 

I suspect the issue that you might be facing is because of settimeout, maybe you are not giving the code a pause to execute the callback method. I hope you do know that JS is single threaded so at any point of time only one path of execution is run, so if a code piece is execting when settimeout callback is called it will wait for the current code to finish before executing the callback. I would suggest refactoring the code and avoid using settimeout if possible, if this check method has to be called after the gettext callback, see if you can embed the call to check within the gettext callback itself. JS code is not sequential but a series of chained callbacks or promises in mordern parlance.

So in short, mujhe to shayad har baar kuch mila 🙂

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

u executed the same code and its working for you....?

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

Yes, you can see for yourself in the screengrab i sent

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

yeah...i see that and i am amazed a bit...actually i am waiting for the current execution still either i am getting blank when i alert(data) and undefined when i write alert(this.data) .if i remove settimeout it runs asychronously .

Votes

Translate

Translate

Report

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 ,
Jun 15, 2020 Jun 15, 2020

Copy link to clipboard

Copied

You are executing the code with a textbox selected right?

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 16, 2020 Jun 16, 2020

Copy link to clipboard

Copied

yes...is there windows or macbook making difference?

Votes

Translate

Translate

Report

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 ,
Jun 16, 2020 Jun 16, 2020

Copy link to clipboard

Copied

I am not very hopeful that a platform issue might happen here. I don't have access to WIN box at the moment so can't verify. However the question remains is this a settimeout issue or a csinterface issue. Did you try what i suggested? Just make plain simple two calls sequentially for getText and then check method. Are you even able to get both the methods called even in this case? If not then it is an InDesign problem, else it means it has something to do with your settimeout or callback chaining, and that is more of a JS question.

 

Anyhow in the current state i don't get what workflow you are trying to make, why is check not in the callback of getText, is it dependent on its result or not, why did you use settimeout in the first place, why is it needed

 

-Manan

Votes

Translate

Translate

Report

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 Beginner ,
Jun 21, 2020 Jun 21, 2020

Copy link to clipboard

Copied

LATEST

ok thanks for replying...i used json method for retuning but wanted to ask if instead of res if i use another function in callback like check in callback of gettext then check will take the ouput of gettext function?

Votes

Translate

Translate

Report

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