0

I am working on a CEP HTML panel for Photoshop and I want to check first if there is any opened documents before my panel do what it has to do. So I made something like this into my index.js so to test if I get the correct result. But psDocumentsLength variable returns as undefined. Any idea what am I doing wrong?

(function()
{
   'use strict';

    var csInterface = new CSInterface();
    var psDocumentsLength; //1//

    function init()
    {
        themeManager.init();
        $(document).ready(function()
        {
            check_PSDocumentsLength();
            alert(psDocumentsLength); //4//
        });
    };

    init();

    function check_PSDocumentsLength() //2//
    {
        var chosenFunction = 'checkDocumentsLength()';
        csInterface.evalScript(chosenFunction, function(result)
        {
            psDocumentsLength = result; //3//
        });
    };

}());
  • Probably an issue of code async. What if you put your alert() in the callback function, near //3//? – Sergey Kritskiy Oct 30 '19 at 18:45
  • @SergeyKritskiy if I put the alert into callback function it works, but my problem is that I can't pass the result out of the callback function to psDocumentsLength variable at //1//!!! :( The alert at //4// it's just for test the psDocumentsLength variable. – Simonetos The Greek Oct 30 '19 at 19:34
0

Well, considering what Sergey Kritskiy said about issue of code async, I tried to add a setTimeout after check_PSDocumentsLength() function call and it worked!!! So my code now looks like this...

    (function()
    {
       'use strict';

        var csInterface = new CSInterface();
        var psDocumentsLength; //1//

        function init()
        {
            themeManager.init();
            $(document).ready(function()
            {
                check_PSDocumentsLength();
                setTimeout(function()
                {
                    alert(psDocumentsLength);
                }, 1000); //4//
            });
        };

        init();

        function check_PSDocumentsLength() //2//
        {
            var chosenFunction = 'checkDocumentsLength()';
            csInterface.evalScript(chosenFunction, function(result)
            {
                psDocumentsLength = result; //3//
            });
        };

    }());
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.