6 Replies Latest reply on Jan 20, 2017 5:15 AM by Loic.Aigon

    Current active document and JSX extension

    Jens Troeger Level 1

      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!

        • 1. Re: Current active document and JSX extension
          Loic.Aigon Adobe Community Professional
          • 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" );

          • 2. Re: Current active document and JSX extension
            Jens Troeger Level 1

            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?

            • 3. Re: Current active document and JSX extension
              Loic.Aigon Adobe Community Professional

              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

              • 4. Re: Current active document and JSX extension
                Laubender Adobe Community Professional & MVP

                Jens Troeger wrote:

                … I assume that value can be any string (What length?) and therewith a JSON string?

                Hi Jens,

                the question about a possible limit of the string length with insertLabel() is a very good one!

                What I have found:

                 

                You can apply a very large string using insertLabel() and you would be able to read out the string with extractLabel().

                 

                The problem:

                If you save, close and reopen the document the string could be truncated.

                The limit of string length is "flexible". Could depend on the InDesign version.

                 

                I did a little experiment with InDesign CS6 v8.1.0 on Mac OSX 10.6.8.

                 

                Opened a new document and applied a label to the document:

                 

                var s = " a a a a a a a a a a";
                for(var n=0;n<12;n++)
                {
                    s = s+s;
                };
                
                $.writeln("s.length "+s.length);
                // Result: 81920
                
                app.documents[0].insertLabel( "LargeString" , s );
                

                 

                Immediately after that I extracted the label and asked for its length:

                 

                app.documents[0].extractLabel("LargeString").length;
                // Result: 81920
                

                 

                Then I saved, closed and opened the document again.

                Same test as before but with a different result:

                 

                app.documents[0].extractLabel("LargeString").length;
                Result: 16384
                

                 

                So if you want to store your JSON with a label be aware of limits.

                Maybe you could distribute your JSON to different labels?

                 

                Regards,
                Uwe

                • 5. Re: Current active document and JSX extension
                  Jens Troeger Level 1

                  Thank you Laubender, that’s good to know! It looks like the maximum saved length is 16kiB. That should be plenty for my purposes...

                   

                  Loic.Aigon, I’ve played with the event callback as you suggested:

                  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

                  } );

                  My current implementation of the event handler retrieves the name of the activated document and then passes that name to the JSX side:

                   

                  csInterface.addEventListener("documentAfterActivate", function (csEvent) {
                      var eventDataXML = csEvent.data; // "<documentAfterActivate><url></url><name>Untitled-1</name></documentAfterActivate>"
                      var docName = $(eventDataXML).find("name")[0].innerText; // TODO Should we test more here, or just assume the layout of the XML? 
                      var script = "getDataFromDocument('" + docName + "');";
                      csInterface.evalScript(script, function (result) {
                          // Do something.
                      });                                                                     
                  });
                  

                   

                  On the JSX side then I use app.documents.findByName(docName) to get a reference to the newly activated document. This seems to work, but looks somewhat clumsy.

                   

                  My question now is: when the event fires, the handler should be able to just work with app.activeDocument, instead of searching for the newly activated document by its name. In the above example, just a var script = "getDataFromActiveDocument();" should replace lines 2-4, no?

                  • 6. Re: Current active document and JSX extension
                    Loic.Aigon Adobe Community Professional

                    Yes, documentAfterActivate implies a document is active so app.activeDocument should be a valid reference at this stage.

                     

                    csInterface.addEventListener("documentAfterActivate", function (csEvent) {  
                    
                      var script = "alert(app.activeDocument.name);";  
                        csInterface.evalScript(script, function (result) {  
                            // Do something.  
                        }); 
                    
                    
                    });