Skip to main content
Jens Troeger
Inspiring
January 19, 2017
Answered

Current active document and JSX extension

  • January 19, 2017
  • 1 reply
  • 4542 views

Hello,

I’m tinkering with a JSX extension following the CEP 7 documentation, and so far it’s working ok. Now, suppose I have multiple documents open in InDesign and open the extension’s panel. Three questions in this context:

  • How do I find the active document in InDesign, the one the user is currently working on? It seems that app.activeDocument, if it exists, is the correct reference?
  • When the user switches from one to another document, I need to update my extension’s panel. Is there a call-back that I could register?
  • Is it possible to store “private” or extension-specific data with the document, so that my extension can update state depending on the active document? Or is that data better stored in local storage using some document-specific identifier?

Thanks!

This topic has been closed for replies.
Correct answer Loic.Aigon
  • How do I find the active document in InDesign, the one the user is currently working on? It seems that app.activeDocument, if it exists, is the correct reference?

Yes but beware this may fail if no documents are open. So you may prefer app.properties.activeDocument that can at least avoid code execution error.

  • When the user switches from one to another document, I need to update my extension’s panel. Is there a call-back that I could register?

Have a look at CInterface.addEventListener in the sdk.

csInterface.addEventListener ( "documentAfterActivate", function(evt) {

       //a document has been activated. You should take care of if in to you jsx handler

    } );

  • Is it possible to store “private” or extension-specific data with the document, so that my extension can update state depending on the active document? Or is that data better stored in local storage using some document-specific identifier?

Documents have ids so you could store info in the local storage However you may prefer the jsx insertLabel method:

app.activeDocument.insertLabel ( "key", "value" );

1 reply

Loic.Aigon
Loic.AigonCorrect answer
Legend
January 19, 2017
  • How do I find the active document in InDesign, the one the user is currently working on? It seems that app.activeDocument, if it exists, is the correct reference?

Yes but beware this may fail if no documents are open. So you may prefer app.properties.activeDocument that can at least avoid code execution error.

  • When the user switches from one to another document, I need to update my extension’s panel. Is there a call-back that I could register?

Have a look at CInterface.addEventListener in the sdk.

csInterface.addEventListener ( "documentAfterActivate", function(evt) {

       //a document has been activated. You should take care of if in to you jsx handler

    } );

  • Is it possible to store “private” or extension-specific data with the document, so that my extension can update state depending on the active document? Or is that data better stored in local storage using some document-specific identifier?

Documents have ids so you could store info in the local storage However you may prefer the jsx insertLabel method:

app.activeDocument.insertLabel ( "key", "value" );

Jens Troeger
Inspiring
January 20, 2017

Thank you Loic.Aigon​!

Documents have ids so you could store info in the local storage However you may prefer the jsx insertLabel method: app.activeDocument.insertLabel ( "key", "value" );

Oh, you refer to the Document.id property?

You’re correct though that insertLabel() seems like a better solution. Will that data be stored when the document is saved? I assume that value can be any string (What length?) and therewith a JSON string?

Loic.Aigon
Legend
January 20, 2017

Hi,

Oh, you refer to the Document.id property?

yep. app.activeDocument.id

But the problem is that ids only remain for the session. Open a doc, save, check id, close. Then add new docs. Reopen doc, now id is different. That's why I would go on insertLabel or xmp meta to tatoo the doc "permanently".

You’re correct though that insertLabel() seems like a better solution. Will that data be stored when the document is saved? I assume that value can be any string (What length?) and therewith a JSON string?

I will be stored given that you save the doc hereafter. Then it can be closed and later reopened, data will still be there as a string.

HTH

Loic

Ozalto | Productivity Oriented - Loïc Aigon