Skip to main content
Kawano Shuji
Inspiring
October 4, 2017
Answered

I wanna get callback as an array object from jsx to PS html panel

  • October 4, 2017
  • 2 replies
  • 3078 views

I`ve developed a Photoshop extension and I try to get callback from jsx

for example

JS

function init() {

        var csInterface = new CSInterface();       

        themeManager.init();

       

               

        $("#btn_test").click(function () {

            csInterface.evalScript('getstatus()',function(f){

       alert(f);     //value is 1,2,3            looks array object but Data Type is String

               

            });

        });

}

JSX

function getstatus(){

var f =[1,2,3];

return f;   

    }

getstatus  return array object but it became String object when evalScript get callback

It`s very inconvenience 

if someone give me good idea I would be glad

Shuji

This topic has been closed for replies.
Correct answer Davide_Barranca12040269

Hi,You

you need to include json2.js in the JSX, for ExtendScript doesn't know about JSON natively.

Then in the JS:

$('#arr').on('click', function(){

    csInterface.evalScript('getArray()', function(res){

        console.log(JSON.parse(res));

    })

})

and in the JSX:

function getArray() {

  return JSON.stringify([1,2,3]);

}

You may want to have a look at my HTML Panel Tips series to get a headstart with similar matters.

Hope this helps!

Cheers,

Davide

2 replies

Ten A
Community Expert
Community Expert
October 4, 2017

ExtendScript function always return String, we need to parse it as David say.

Kawano Shuji
Inspiring
October 5, 2017

I didn't know that ExtendScript always return String
I tried to it without Json2 many time

Davide_Barranca12040269
Legend
October 4, 2017

Hi,You

you need to include json2.js in the JSX, for ExtendScript doesn't know about JSON natively.

Then in the JS:

$('#arr').on('click', function(){

    csInterface.evalScript('getArray()', function(res){

        console.log(JSON.parse(res));

    })

})

and in the JSX:

function getArray() {

  return JSON.stringify([1,2,3]);

}

You may want to have a look at my HTML Panel Tips series to get a headstart with similar matters.

Hope this helps!

Cheers,

Davide

Davide Barranca - PS developer and authorwww.ps-scripting.com
Marc Autret
Legend
October 5, 2017

Hi Davide,

It should be noted that Crockford's json2.js is not fully ExtendScript compliant. (More precisely, ExtendScript is not fully ECMA compliant, as you already know.)

In particular, json2 uses a few ternary operations in the form

cond1 ? "cond1 OK" : cond2 ? "cond2 OK" : "cond2 KO"

assuming right-associativity of the ternary op, which is the case in ECMA-262 but not in ExtendScript :-/

Studying json2 some months ago I found other tricky issues, which leads me to implement IdExtenso's JSON module:

IdExtenso/$$.JSON.jsxlib at master · indiscripts/IdExtenso · GitHub

Best,

Marc

Davide_Barranca12040269
Legend
October 5, 2017

Thank you Marc!

Isn't your JSON implementation plug-and-play replacement of json2.js, correct?

Davide Barranca - PS developer and authorwww.ps-scripting.com