3 Replies Latest reply on Jan 29, 2017 9:04 AM by tusharde

    HTML panel: JSX to JS: saving object to a variable in the js file

    tusharde

      I'm trying to save the documents measurement units in a variable in the main.js file for later use in the script. I am grabbing that information by passing an object in the callback function of a csinterface.evalscript('doc_info()', callback).

       

      This is in the main.js file.

      var doc_info = {}; // NEED TO SAVE THE OBJECT FROM THE JSX file for later use.
      
      function get_doc_info() {
           csInterface.evalScript('doc_info()', function(result) {
                var o = JSON.parse(result);
                doc_info = {
                    h_units: o.h_units,
                    v_units: o.v_units,
                };
                alert(doc_info.h_units); // THIS WORKS
           });
      }
      
      alert(doc_info.h_units); //THIS DOES NOT WORK. I GET "UNDEFINED".
      

       

      This is in the indesign.jsx file.

      function doc_info() {
          var obj = {
              h_units: app.activeDocument.viewPreferences.verticalMeasurementUnits.toString(),
              v_units: app.activeDocument.viewPreferences.horizontalMeasurementUnits.toString()
          }
          return JSON.stringify(obj);
      }
      

       

      The issue I am having is that I can console.log/alert the results from inside the callback function. But when I try to save the results to the var doc_info and then try to access doc_info outside the callback function I get "undefined".

       

      I read somewhere on this forum or David Barrancas website that it might be because of the asynchronous nature of csinterface.evalscript(). Is that the reason? What is the solution for this problem?

       

      Thanks