2 Replies Latest reply on Dec 15, 2015 7:48 AM by Loic.Aigon

    Close window before re-opening

    Bo Dudek

      Hi

       

      I'm making a script, which opens a window with the following lines

       

      var modal = new Window('palette', 'My Modal');

      modal.frameLocation = [200,200];

      modal.show();

       

      - but in several cases it makes sense to the user to run script multiple times, but when run the 2nd time, then there are 2 windows.

      Is there a way to avoid this by 'looking' for existing windows opened by my script and close them before opening a new?

       

      Thanks a lot

      Bo Dudek

      Webdeveloper

        • 1. Re: Close window before re-opening
          Mikie9 Level 1

          This will close all open documents:

           

          if(app.documents.length > 0){

                      app.documents.everyItem().close(SaveOptions.NO);

                      }

           

           

          If you only need the front document closed then:

           

          if(app.documents.length > 0){

                      app.activeDocument.close(SaveOptions.NO);

                      }

          • 2. Re: Close window before re-opening
            Loic.Aigon Adobe Community Professional

            That kind of issue is generally related to teh scope where you created the window. If the window is on the global object, you should'nt have any duplicates. If on the contrary if the palette is built and called in a function scope, every call wil outpout a new window.

             

            This one is built and shown in the global scope. No worries.

            #targetengine 'myUniqueWindow'
            var modal = new Window('palette', 'My Modal');
            var b = modal.add('button', undefined, 'Close' );
            b.onClick = function(){
            modal.close(0);    
            }
            modal.frameLocation = [200,200];
            
            modal.show();
            

             

            This one is built and shown in a function scope leading to duplicates…worries.

             

            #targetengine 'myUniqueWindow'
            var build = function () {
            var _modal = new Window('palette', 'My Modal');
            var b = _modal.add('button', undefined, 'Close' );
            b.onClick = function(){
            _modal.close(0);    
            }
            _modal.frameLocation = [200,200];
            
            _modal.show();
            
            }
            
            
            build()
            

             

            Eventually, this one is built in a function scope but the window object is returned. So we can reference teh window to itself is formerly built then show it in a outer scope:

             

            #targetengine 'myUniqueWindow2'
            var build = function () {
            var _modal = new Window('palette', 'My Modal');
            var b = _modal.add('button', undefined, 'Close' );
            b.onClick = function(){
            _modal.close(0);    
            }
            _modal.frameLocation = [200,200];
            
            return _modal
            
            }
            
            
            var modal = modal || build();
            modal.show();
            

             

            HTH

             

            Loic

            www.ozalto.com