Search code examples
jqueryjsonjsxphotoshopcomplex-event-processing

Photoshop 2015 extension not able to access all array items


I am a novice programmer and new to adobe extensions but have developed an extension that is not working correctly in Photoshop 2015. It works perfectly in PS 2017, 2018 and 2019.

I am reading array data from a JSON file with JSX and using an event listener in the main.js file to return the data. The extension reads the settings from the returned array. It works perfectly except in PS 2015 where it can only return the UserID but nothing else.

JSX Code reading data from a local file:

function ReadUserStoredDataFromFile(vfilepath){      
    var rd = ReadFileData(vfilepath);     
    SendDataToListenerJS("getuserstoredinfo",rd);    
}

function ReadFileData(vfullpath){    
    fle = File(vfullpath); 
    var filedata = 0;

    if(fle.exists){    
        fle.open('r');
        filedata = fle.read();
        fle.close();
    }

    return filedata;
}

function SendDataToListenerJS(JSlistenerName,DatatoJS){
    try {
        var xLib = new ExternalObject("lib:\PlugPlugExternalObject");
    } catch (e) {
        alert(e);
    }

    if (xLib) {
        var eventObj = new CSXSEvent(); 
        eventObj.type = JSlistenerName;
        eventObj.data = DatatoJS;
        eventObj.dispatch();
    }
}

INDEX.HTML

var datareceived_arry = new Array();

MAIN.JS Code

var usermydocuments = csInterface.getSystemPath(SystemPath.MY_DOCUMENTS);

setTimeout(AutoStatus, 1000);  

function AutoStatus(){
    var usdocfp = '"' + usermydocuments + '/rt/autostatus.json' + '"';
    csInterface.evalScript('ReadUserStoredDataFromFile('+ usdocfp +')');
}

csInterface.addEventListener("getuserstoredinfo", function(evt) {              
    datareceived_arry = evt.data;              
    var unoti = GetAllNotifications();                        
});

function GetAllNotifications(){          
            //get user notification
            var allunotifications = "none";

            //add userid
            allunotifications = "<b>Userid: " + datareceived_arry['userinfo']['userid'] + "</b>";

            alert(datareceived_arry['userinfo']['userid']); //This shows the userid Fari
            alert(JSON.stringify(datareceived_arry['apps'][0]['version'])); //This shows undefined
            alert(JSON.stringify(datareceived_arry)); //This shows all the data
            $('#showdata').html(JSON.stringify(datareceived_arry)); //This shows all the data
}

Here is the data saved to file:

{"0":0,"userinfo":{"userid":"fari","loginstatus":1,"usernotification":""},"apps":[{"appid":"rt5processor","accounttype":"Admin","status":"6","datestarted":"2019-10-11","usagedata":"1","flagcookiedata":"0","flagstorage":"0","appname":"RTPROCESSOR","version":"1.11.8","appnotification":"","apptype":"extension","updateflag":"0"},{"appid":"rt5sharpen","accounttype":"Admin","status":"6","datestarted":"2019-10-11","usagedata":"1","flagcookiedata":"0","flagstorage":"1","appname":"RTSHARPEN","version":"1.11.8","appnotification":"","apptype":"extension","updateflag":"0"}],"tutorials":[{"appid":"rtp","tutorialurl":"https://www.youtube.com/watch?v=W3cKq7S3qKc","featureonapp":"sharpen3"},{"appid":"rtp","tutorialurl":"https://www.youtube.com/watch?v=fKn5fG3M1m8","featureonapp":"Sharpen"},{"appid":"rtp","tutorialurl":"https://www.youtube.com/watch?v=m9M7J9uMrJk","featureonapp":"sharpen2"}],"misc":{"globalnotification":""}}

I used dummy test data which will be replaced. It returns perfectly fine in PS 2017,2018,2019. It utilizes cep 4 but I have tried cep 6 and the issue remains. Any help appreciated.


Solution

  • Maybe there's a great story behind this, but apparently event object parser in CC2015 is broken: while simple object work (so your datareceived_arry['userinfo']['userid'] is accessible) arrays are converted to strings: JSON that the panel receives looks like this (notice all the arrays are strings now):

    {
        "0": "0",
        "tutorials": "[{\"tutorialurl\":\"https://www.youtube.com/watch?v=W3cKq7S3qKc\",\"appid\":\"rtp\",\"featureonapp\":\"sharpen3\"},{\"tutorialurl\":\"https://www.youtube.com/watch?v=fKn5fG3M1m8\",\"appid\":\"rtp\",\"featureonapp\":\"Sharpen\"},{\"tutorialurl\":\"https://www.youtube.com/watch?v=m9M7J9uMrJk\",\"appid\":\"rtp\",\"featureonapp\":\"sharpen2\"}]",
        "userinfo":
        {
            "usernotification": "",
            "userid": "fari",
            "loginstatus": "1"
        },
        "apps": "[{\"version\":\"1.11.8\",\"datestarted\":\"2019-10-11\",\"appname\":\"RTPROCESSOR\",\"updateflag\":\"0\",\"flagstorage\":\"0\",\"accounttype\":\"Admin\",\"appid\":\"rt5processor\",\"status\":\"6\",\"usagedata\":\"1\",\"flagcookiedata\":\"0\",\"apptype\":\"extension\",\"appnotification\":\"\"},{\"version\":\"1.11.8\",\"datestarted\":\"2019-10-11\",\"appname\":\"RTSHARPEN\",\"updateflag\":\"0\",\"flagstorage\":\"1\",\"accounttype\":\"Admin\",\"appid\":\"rt5sharpen\",\"status\":\"6\",\"usagedata\":\"1\",\"flagcookiedata\":\"0\",\"apptype\":\"extension\",\"appnotification\":\"\"}]",
        "misc":
        {
            "globalnotification": ""
        }
    }
    

    if you really want to use PlugPlugExternalObject to pass data from jsx to js for some reasome I believe you'll have to write a custom parser for your object. Otherwise I'd suggest to simply return data from jsx to js and use it in a callback:

    JSX:

    function ReadUserStoredDataFromFile(vfilepath)
    {
        var rd = ReadFileData(vfilepath);
        return rd;
    }
    
    function ReadFileData(vfullpath)
    {
        fle = File(vfullpath);
        var filedata = 0;
    
        if (fle.exists)
        {
            fle.open('r');
            filedata = fle.read();
            fle.close();
        }
    
        return filedata;
    }
    

    JS:

        var datareceived_arry = [];
        var usermydocuments = csInterface.getSystemPath(SystemPath.MY_DOCUMENTS);
    
        setTimeout(AutoStatus, 1000); 
    
        function AutoStatus()
        {
            var usdocfp = '"' + usermydocuments + '/rt/autostatus.json' + '"';
            csInterface.evalScript('ReadUserStoredDataFromFile(' + usdocfp + ')', function(res)
            {
                datareceived_arry = JSON.parse(res) // result comes as a string
                //get user notification
                var allunotifications = "none";
    
                //add userid
                allunotifications = "<b>Userid: " + datareceived_arry['userinfo']['userid'] + "</b>";
    
                alert(datareceived_arry['userinfo']['userid']); //This shows the userid Fari
                alert(JSON.stringify(datareceived_arry['apps'][0]['version'])); //This shows undefined
                alert(JSON.stringify(datareceived_arry)); //This shows all the data
                $('#showdata').html(JSON.stringify(datareceived_arry)); //This shows all the data
            });
        }