Skip to main content
K.Daube
Community Expert
Community Expert
April 28, 2016
Answered

How to detect change of document ?

  • April 28, 2016
  • 1 reply
  • 1384 views

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

This topic has been closed for replies.
Correct answer Klaus Göbel

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;   

          }

      }   

    }  

1 reply

frameexpert
Community Expert
Community Expert
April 28, 2016

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

www.frameexpert.com
K.Daube
Community Expert
K.DaubeCommunity ExpertAuthor
Community Expert
April 29, 2016

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.

Klaus Göbel
Klaus GöbelCorrect answer
Legend
April 29, 2016

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;   

          }

      }   

    }