5 Replies Latest reply on May 2, 2013 3:56 AM by Vamitul

    Using global variables across scripts

    s.vnesh Level 1

      Hi,

       

      I have a SOAP client which executes runScript method.

      Three SOAP requests, one after the other, call runScript and execute a corresponding jsx on server.

      Can I have some session/global-wide variables that I can access across scripts?

      For example, during execution of first script, I set some variable. And again during the second and third SOAP requests, I use the variable (set in 1st script) in the 2nd and 3rd scripts.

       

      How can I achieve the above?

       

      I would really appreciate some help on this.

        • 1. Re: Using global variables across scripts
          -hans- Level 4

          insertLabel:

           

           

          #target indesign
          
          var objLabel = {
              house:'mouse'
              }
          
          app.insertLabel('appLabel', objLabel.toSource());
          var currObj = eval(app.extractLabel('appLabel'));
          alert(currObj.house)
          
          1 person found this helpful
          • 2. Re: Using global variables across scripts
            Vamitul Level 4

            what hans said, and also, i think global variables are persistent in the same target engine

            • 3. Re: Using global variables across scripts
              fabianmoronzirfas Level 1

              Fo exchanging values between scripts you can make use of a targetengine.

              Script one

                    #targetengine "session01"
                var myValue = 0; // new value
                    alert(myValue); // result is 0
                    myValue++; // increment by 1
              

               

              Script two

                    #targetengine "session01"
                  alert(myValue); // result is 1
              
              1 person found this helpful
              • 4. Re: Using global variables across scripts
                vinothr82 Level 1

                You can use either of the methods #targetengine or insertLabel.

                 

                One demerit, I would say, of #targetengine "session01" is that the specific session will exists as long as InDesign is running, which is not desirable. Those will destroy only when you quit your application. I don't think there is any garbage collection to rescue here.

                However, insertLabel and extractLabel can be used on specifc DOM objects, as per your need, including the app object.

                 

                If you want to share variables across scripts on per-document basis then you can store the variables on the document object:

                doc.insertLabel("name", "value");

                 

                So, once the document is closed all the labels are gone. You can decide according to your requirement whether you want application-level globals (then use app.insertLabel), document level (then doc.insertLabel), spread level, etc.

                • 5. Re: Using global variables across scripts
                  Vamitul Level 4

                  if you use the insert/extract label, you can use myObject.toString() to serialize an entire object, and when you retrive the label use myObject=eval(myLabelString). The same trick is usefull to store script preferences.