12 Replies Latest reply on Nov 25, 2011 2:24 PM by Loic.Aigon

    [JS] Avoid several palettes instanciation

    Loic.Aigon Adobe Community Professional

      Hi all,

       

      I feel so dumb asking for such a silly question but I really can't get this one to work. I have a simple dialog (a palette). Thing is that is user clicks several times, the script outputs many windows. I want to check for a former launched palette.

      I did try everything such a global variable to edit onShow and onClose (isRunning = false), private variables.

       

      Any hint ?

       

       

      #targetengine 'demo'
      
      var myDialog = (function(){
          var w = new Window('palette');
          return{
              show:function(){
                  w.show();
              }
          }
      })();
      
      myDialog.show();
      

       

       

      Option 1: Global variable

       

      #targetengine 'demo'
      
      var isRunning = false;
      
      var myDialog = (function(){
          var w = new Window('palette');
      
           w.onShow = function(){ isRunning = true; }
      
           w.onClose = function(){ isRunning = false; }
      
          return{
              show:function(){
                  if(!isRunning) { w.show(); }
              }
          }
      })();
      
      myDialog.show();
      

       

      Option 2: private variable:

       

      #targetengine 'demo'
      
      
      
      var myDialog = (function(){
           var isRunning = false;
          var w = new Window('palette');
      
           w.onShow = function(){ isRunning = true; }
      
           w.onClose = function(){ isRunning = false; }
      
          return{
              show:function(){
                  if(!isRunning) { w.show(); }
              }
          }
      })();
      
      myDialog.show();
      
      

       

      I can't get it working so the palette isn't run if done already. I also tried to check for some dialog item existence like in this page but it's not helping : http://kelsocartography.com/scripts/scripts/nvkelso/registrationButtons.jsx

       

      TIA for any tip. I am certain it's so obvious.

       

      Loic

        • 1. Re: [JS] Avoid several palettes instanciation
          absqua Level 4

          Weirdly, this kind of construction will prevent two from running at once:

           

          #targetengine 'demo'
          
          function SpecialPalette(){
               var isRunning = false;
              var w = new Window('palette');
          
              w.okBtn = w.add('button', undefined, "OK");
              w.cancelBtn = w.add('button', undefined, "Cancel");
          
              w.defaultElement = w.okBtn;
              w.cancelElement = w.cancelBtn;    
          
               w.onShow = function(){ isRunning = true; }
          
               w.onClose = function(){ isRunning = false; }
          
              this.show = function(){
                  if(!isRunning){
                      w.show();
                  }
                  else{
                      $.writeln("Already running.");
                  }
              };
          }
          
          var myDialog = myDialog || new SpecialPalette();
          
          myDialog.show();
          

           

          However, on subsequent runnings in the same session, the buttons no longer work! (So, if you have no buttons and your palette doesn't do anything, this approach will work. ) I would love some clarification of the way ScriptUI fails to work in this kind of scenario, and why.

           

          Jeff

          • 2. Re: [JS] Avoid several palettes instanciation
            John Hawkinson Level 5

            Why are you making it so complicated? What's wrong with your global variable and then

             

            if (!isRunning) { myDialog.show(); }

             

            ?

            • 3. Re: [JS] Avoid several palettes instanciation
              absqua Level 4

              Actually, I guess it's not so weird. You're remaking your myDialog variable at each invocation, so isRunning is false every time. Just adding a check to see if myDialog already exists will prevent it from running two at once:

               

              #targetengine 'demo'
              
              var myDialog = myDialog || (function(){
                   var isRunning = false;
                  var w = new Window('palette');
              
                   w.onShow = function(){ isRunning = true; }
              
                   w.onClose = function(){ isRunning = false; }
              
                  return{
                      show:function(){
                          if(!isRunning) { w.show(); }
                      }
                  }
              })();
              
              myDialog.show();
              

               

              But, I think you'll have the same problems I showed in my first post with things not working when you run it later in the same session.

              • 4. Re: [JS] Avoid several palettes instanciation
                Loic.Aigon Adobe Community Professional

                That's all the point, John. I did use a global and affected it while showing and closing window. So at the time of launch, I check for isRunning.

                The problem is that it doesn't work here (I mean my place)

                 

                Capture d’écran 2011-11-15 à 16.37.30.png

                • 5. Re: [JS] Avoid several palettes instanciation
                  Loic.Aigon Adobe Community Professional

                  Hi absqua,

                   

                  Thanks for you input. I will keep your approch in mind but as you told me buttons may become inoperant, I admit this is an issue worse for me than having multiple windows

                   

                  Thx anyway

                   

                  Loic

                  • 6. Re: [JS] Avoid several palettes instanciation
                    Loic.Aigon Adobe Community Professional

                    Hi absqua,

                     

                    Thanks again for the input. I was fearing something like this

                     

                    I will try to get a way

                     

                    Loic

                    • 7. Re: [JS] Avoid several palettes instanciation
                      Loic.Aigon Adobe Community Professional

                      absqua wrote:

                       

                      Actually, I guess it's not so weird. You're remaking your myDialog variable at each invocation, so isRunning is false every time. Just adding a check to see if myDialog already exists will prevent it from running two at once:

                       

                      #targetengine 'demo'
                       
                      var myDialog = myDialog || (function(){
                           var isRunning = false;
                          var w = new Window('palette');
                       
                           w.onShow = function(){ isRunning = true; }
                       
                           w.onClose = function(){ isRunning = false; }
                       
                          return{
                              show:function(){
                                  if(!isRunning) { w.show(); }
                              }
                          }
                      })();
                       
                      myDialog.show();
                      

                       

                      But, I think you'll have the same problems I showed in my first post with things not working when you run it later in the same session.

                       

                       

                      Hi absqua,

                       

                      I juts retested and you got it right. I think I had issues with the running sessions. I restarted InDesign several times but probably not in the good tempo. However, now it's working.

                       

                      Thanks to Marc too for getting me back to this lead.

                       

                      Loic

                      • 8. Re: [JS] Avoid several palettes instanciation
                        absqua Level 4

                        I'm glad that worked for you Loic. When I'm testing scripts that run in persistent engines I usually increment the #targetengine directive after every edit ('demo', 'demo1', 'demo2', etc.) so I don't have to restart InDesign.

                         

                        Please let us know if you experience the problems I talked about with events not being handled on subsequent runnings of the script during the same session, and if you're able to work around them. I have not been able to get that to work without rebuilding the palette on every invocation.

                         

                        Jeff

                        1 person found this helpful
                        • 9. Re: [JS] Avoid several palettes instanciation
                          Loic.Aigon Adobe Community Professional

                          Why didn't you write that 2hours ago ? I can't count how many restarts I did this afternoon

                           

                          thanks a lot for that really helpful tip

                           

                          Loic

                          • 10. Re: [JS] Avoid several palettes instanciation
                            Loic.Aigon Adobe Community Professional

                            Hi Jeff,

                             

                            I will if I have troubles

                            • 11. Re: [JS] Avoid several palettes instanciation
                              Peter Kahrel Adobe Community Professional & MVP

                              Another method (and a simpler one, it seems to me) is to see if the palette exists:

                               

                              if(Window.find ("palette", "demopalette") == null)
                                  {
                                  var w = new Window ("palette", "demopalette");
                                  // etc.
                                  w.show();
                                  }
                              
                              

                               

                              Peter

                              1 person found this helpful
                              • 12. Re: [JS] Avoid several palettes instanciation
                                Loic.Aigon Adobe Community Professional

                                Hi Peter and thx for this one. Also really valuable tip.

                                But looks like object = object || object creation is working really fine