5 Replies Latest reply on Oct 26, 2012 5:31 AM by Pickory

    called twice

    Pickory Level 3



      What happens if your script is called twice?


      If you have a script that creates a palette on call 1. On call 2, your code determines that the palette is already showing, calls a routine and then returns without creating the palette. In my case, in call 2 we look at some parameters to determine not to show the palette.


      Are there actually two copies of the script running? What heppens to any memory allocated in call 2. Is it safe to keep calling the same script and exiting as we do in call 2.


      Is there some documentation on this sort thing.





        • 1. Re: called twice
          Peter Kahrel Adobe Community Professional & MVP

          Not entirely sure that I understand what you mean. You ran a script that added a palette. At some later point, when the palette is still showing, you run the script again. You say "in call 2 we look at some parameters to determine not to show the palette." So you determine that the palette shows and you decide not to do anything.


          Then you ask "Are there actually two copies of the script running?" This I don't follow: you decided not to show the palette in call 2, so it seems to me not relevant to ask if the second script is running.



          • 2. Re: called twice
            Harbs. Level 6

            I'm not sure what you mean either, but each time a script is run, the code is parsed and committed to memory. If you are just calling an existing function the second time around, it will execute the orignial parseed code.


            I have no idea if this answers your  question or not...

            • 3. Re: called twice
              Pickory Level 3

              Peter, Harbs, thank you for your replies.


              Here is what we are doing.


              Call 1, create the palette.


              Call 2, we pass a paramater to the script asking it to select a different row in the palette, then we exit.


              Both calls are made by a plugin, call 2 could happen many times.

              • 4. Re: called twice
                Marc Autret Level 4

                Hi Pickory,


                When you run a script in a persistent engine (i.e. any targetengine which is not 'main'), the global scope (i.e. this, or $.global) maintains its data in memory after the script terminates, so that all global variables/references are preserved for a next run. (By contrast, the 'main' engine automatically kills all references.)


                A quick test to check this fact:


                // ==================
                // Run this script twice
                // ==================
                #targetengine "test"
                var arr, obj, func;
                if( 'undefined'==typeof arr )
                    alert( "Defining arr" );
                    arr = [1,2,3];
                if( 'undefined'==typeof obj )
                    alert( "Defining obj" );
                    obj = {foo:'bar'};
                if( 'undefined'==typeof func )
                    alert("Defining func");
                    func = function(){alert( "Hello World!" )};
                alert( [arr,obj.toSource(),func.toString()].join('\r') );


                As you can see, the global variables arr, obj, func are still here when you re-run the script, and you don't need to define them again.


                Note that the script in itself is not stored in the 'test' engine. The code is still entirely interpreted when you run it, and you could run another script in that same engine. Hence you can see a persistent engine as a "memory space" specifically allocated to your task and which persists during all the InDesign session.


                What if you redefine again and again the same global data, e.g:


                #targetengine 'test2'

                var a = [1,2,3];

                // etc


                Theoretically this leads to potential memory leaks, as each time the script runs the existing content of the array is lost without deallocation as a new one is created. Fortunately, the "garbage collector" mechanism is processed in background task so old references are supposed to be cleaned. However, there is an obvious performance cost in permanently recreating and feeding complex data (arrays, objects, functions) that already exist. That's why it is a good practice to check whether global vars are already defined. Some of them are not intended to vary, others may require to be updated when the context changes. When a persistent-engine script becomes complex, an usual programing pattern is to write a global loader routine as follows:


                #targetengine 'test3'


                var loaded;

                if( !loaded )


                   // create and feed global vars

                   loaded = 1;


                // your script here


                Hope that helps.




                • 5. Re: called twice
                  Pickory Level 3

                  Marc, Peter and Harbs, Thank you!