4 Replies Latest reply on Nov 24, 2017 4:01 AM by Marc Autret

    "Main" and "Session" Targetengine Paradox.

    Trevorׅ Adobe Community Professional

      1) I need to use a "session" targetengine because I a using a "window" type UI window which like the "palette" type cannot be run using the "main" target engine.


      2) I want however that on the press of a button on the UI a script should be run in the "main" targetengine as it uses a lot of memory which I want dumped on conclusion of the script's execution.


      3) I would like to avoid using 2 separate files one with the UI which activates the other file with the main function using the doScript command but my feeling is that this might be the only option.  I would however like to know how to do this as I don't know .


      The script below illustrate one of my pathetic (failed) attempts at solving the problem.


      Would much appreciate help on the problem





      #target "InDesign"
      #targetengine "session"
      target = "#target 'InDesign'";
      targetengine="#targetengine 'main'";
      w = new Window ("window", undefined, undefined,{ resizeable: true, borderless: false} );
      myPanel = w.add ("panel");
      w.add ("statictext", undefined, "main");
      mb1=w.add ("button", undefined, "Press here!");
      mb1.onClick = function () {
          app.doScript(target,ScriptLanguage.javascript, undefined, UndoModes.entireScript);
          app.doScript(targetengine,ScriptLanguage.javascript, undefined, UndoModes.entireScript);
          app.doScript(test, ScriptLanguage.javascript, undefined, UndoModes.entireScript);}
      myVarm = "session target engine";
      function test(){
      alert("If you don't see this alert then the function is not being executed");
      ww = new Window ("window", undefined, undefined,{ resizeable: true, borderless: false} );
      myStatic = ww.add ("statictext", [0, 0, 700, 100], "If this stays on the screen then the target engine has not been changed to \"main!\"\r\rIf You can get this message not to stay on the screen then you are a better scripter than me\r\rThis is not a very difficult achivement !!",{multiline: true });
      myStatic.justify = "center";
      myMainVar = "Should so error if value is tried to be got from the data browser after the script has run";
        • 1. Re: "Main" and "Session" Targetengine Paradox.
          Marc Autret Level 4

          I'm afraid you need to embed the whole code as a string in a single app.doScript (including the #targetengine directive).

          Since the test function is declared in the session engine, it is unknown from the main engine, so you probably have to pass the body through func.toSource() and to call the func:



          #targetengine "mySession"
          w = new Window("window", undefined, undefined, {resizeable:true, borderless:false} );
          myPanel = w.add ("panel");
          w.add("statictext", undefined, "main");
          mb1=w.add("button", undefined, "Press here!");
          mb1.onClick = function()
              app.doScript("#targetengine 'main'\r"+test.toSource()+"();",
              ScriptLanguage.javascript, undefined, UndoModes.entireScript);
          function test(){alert($.engineName);}




          • 2. Re: "Main" and "Session" Targetengine Paradox.
            Trevorׅ Adobe Community Professional

            Thanks a lot Marc, works great.


            I think it would have taken me a few years to figure out the toSource bit!


            My next stage is to figure out how to transfer variables to the "main" session, which I see is discussed in the scripting guide using “script arguments”.

            • 3. Re: "Main" and "Session" Targetengine Paradox.
              Trevorׅ Adobe Community Professional

              Hi Marc and all


              I accidentally came across this post while searching for something and came to Indiscripts :: InDesign Scripting Forum Roundup #3  which reminded me of this post.

              There is one major issue with the toSource() method, being that it fits the whole function on one line.


              Let's look at the following to functions.


              function test1(){
              function test2(){
                  // some annotation here


              These functions get reduced using the toSource() method to a single line.


              (function test1(){  alert(1)  alert(2) })
              // Fails because of the lack of a ';' between the closing alert 1 bracket and alert 2
              (function test2(){  // some comment  alert(1) })
              // fails because of // some comment


              Using the toString() method does not  reduce the function to a single line rather they are left as is.

              As such in order to execute them one merely has to add the encasing brackets.


              app.doScript("#targetengine 'main'\r("+test.toString()+")();", ScriptLanguage.javascript, undefined, UndoModes.entireScript);


              I would of left a comment on your site but there was no option so I shall leave that for you.



              2 people found this helpful
              • 4. Re: "Main" and "Session" Targetengine Paradox.
                Marc Autret Level 4

                Hi Trevor,


                Thanks a lot. I've added a note in my 2012's entry.