4 Replies Latest reply on Apr 30, 2016 3:03 AM by K.Daube

    How to detect change of document ?

    K.Daube Level 1

      Dear friends and experts,

      It seems obvious that with my new project I have opened the box of Pandora...

      Palettes stay open even when setting a new cursor location, hence I use this for my dialogue.

      But the user can also switch to another document while the palette is open.

      Currently I have a button Refresh and the user is urged to use it after switching to another document. The function behind is to analyse the newly entered document and define global variables, update the dialogue.

      How could I detect the switch of the document ? I have not found something like onDocumentChange.

      (In the current script a check for changed document name is in the btnRefresh.onClick function to avoid irrelevant work).

       

      Thank You for any ideas.

      Klaus

        • 1. Re: How to detect change of document ?
          frameexpert Level 4

          Hi Klaus,

           

          There is a notification you can use: Constants.FA_Note_PostActiveDocChange

           

          You set it up something like this in your script:

           

          // Set the notification for after the active document changes.
          Notification (Constants.FA_Note_PostActiveDocChange, true);
          
          // Built-in function that is called when a event is triggered.
          function Notify (note, object, sparam, iparam) {
             
              // Handle the event triggered after the active document changes.
              switch (note) {
                 
                  case Constants.FA_Note_PostActiveDocChange:
                      // Update your dialog box here.
                      break;
              }
          }
          

           

          -Rick

          • 2. Re: How to detect change of document ?
            K.Daube Level 1

            Thanks You, Rick

            In theory I knew that from the ExtendScript documentation - but does not know how to implement.

            Encouraged by your post I tried this:

            #target framemaker
            Notification (Constants.FA_Note_PostActiveDocChange, true);  
            var globalValue = app.ActiveDoc.Name;
            var winPal = new Window ("palette", "Simple Palette", undefined);
             
            SimplePalette (globalValue);
            
            function SimplePalette (outerParm) {
              var innerParm = "none";
              var buttonOuter = winPal.add ("button", undefined, "Outer");
              var buttonInner = winPal.add ("button", undefined, "Inner");
              var st0 = winPal.add ("statictext", undefined, "st= " + innerParm);
                  st0.preferredSize.width = 300;
              var st1 = winPal.add ("statictext", undefined, "Button clicked: " + innerParm);
            
              buttonOuter.onClick = function () {
                st1.text = "Button clicked: Outer";
              }
            
              buttonInner.onClick = function () {
                st1.text = "Button clicked: Inner";
                st0.text = outerParm;                                // this changes the display
              }
            
              winPal.show ();
            }
            
            function Notify (note, object, sparam, iparam) {  
            // Handle the event triggered after the active document changes.
            $.br(true);
              switch (note) {  
                case Constants.FA_Note_PostActiveDocChange:  
                  // Update your dialog box here. 
                  winPal.st1.text = "Current document is " + object.Name ; 
                  winPal.show ();
                  break;  
              }  
            } 
            

            I have two documents open when I start the script. Clicking button Inner places the current document name.

            When switching the document I expected to run into the breakpoint - but nothing happens.

            Is function Notify at a wrong place - and may it have a different name?

            Thank You for clarifying this.

            • 3. Re: How to detect change of document ?
              Klaus Göbel Level 3

              Hi Klaus,

               

              this version of your script works fine:

               

              Just remove some "var" in winPal - dialog.

              Remove "winPal.show ()" in function Notify

              give a little more space to st1.

               

                 #target framemaker  
                  Notification (Constants.FA_Note_PostActiveDocChange, true);    
                  var globalValue = app.ActiveDoc.Name;  
                  var winPal = new Window ("palette", "Simple Palette", undefined);  
                     
                  SimplePalette (globalValue);  
                    
                  function SimplePalette (outerParm) {  
                     innerParm = "none";  
                     buttonOuter = winPal.add ("button", undefined, "Outer");  
                     buttonInner = winPal.add ("button", undefined, "Inner");  
                     st0 = winPal.add ("statictext", undefined, "st= " + innerParm);  
                     st0.preferredSize.width = 300;  
                     st1 = winPal.add ("statictext", undefined, "Button clicked: " + innerParm);  
                    st1.preferredSize.width = 300; 
                    buttonOuter.onClick = function () {  
                      st1.text = "Button clicked: Outer";  
                    }  
                    
                    buttonInner.onClick = function () {  
                      st1.text = "Button clicked: Inner";  
                      st0.text = outerParm;                                // this changes the display  
                    }  
                    
                    winPal.show ();  
                  }  
                    
                  function Notify (note, object, sparam, iparam) {    
                  // Handle the event triggered after the active document changes.  
                  $.bp(true);  // here was a type : $.br(true) 
                    switch (note) {    
                      case Constants.FA_Note_PostActiveDocChange:    
                          {
                        // Update your dialog box here.   
                        st1.text = "Current document is " + object.Name ;   
                        break;    
                        }
                    }    
                  }   
              
              • 4. Re: How to detect change of document ?
                K.Daube Level 1

                Thanks, Klaus, yes it really does what it should.

                I forgot to remove the var after moving the window declaration out of the dialog function

                But it is impossible to debug function Notify:

                1. Just running the script with F5 in ESTK does not honour the breakpoint in line 30.
                2. Using step into (F11) suddenly creates a new script file when i reach line 2.
                3. The new script file contains this highlighted line:
                  $.fileName
                4. Stepping further creates another new script file containing this highlighted line:
                  (new File("/e/_DDDprojects/FM-calc/test-scripts/OnDocumentChangeTest2.jsx")).fsName
                5. Stepping further creates another new script file containing this highlighted line:
                  $.line
                6. Stepping further eventually brings me back into my script to line 3.
                7. More steps with F11 eventually displays the dialog.
                8. Klicking buttuns do what they should
                9. Changing the document displays the new document name in the dialogue.
                  But the breakpoint (ah, there was typo again) is not honoured. No return into ESTK.

                It seems that I have to live with this behaviour - maybe an alert would provide information if needed.

                Thank You for your corrections which finally led to a working script!