7 Replies Latest reply on May 22, 2017 12:32 PM by Thom Parker

    How to tell which document is active

    fintml

      Hi,

       

      Is there a way to tell which doc object is active? Or whether the Acrobat program is active or not?

       

      ------------------------------------------------------

       

      The situation is following:

       

      1. I have documents A and B

       

      2. When opened A starts an interval with app.setInterval("PROCEDURE();", 100)

       

      3. I would like the repeatedly run PROCEDURE-method to do different things depending on the active document (A or B)

       

      Can this be achieved using javascript? If there would be a method like app.activeDocument() that'd return the active document, then I would be able to distinguish between A and B, but I can't find anything of the sort...

       

      -----------------------------------------------------------------

       

       

      Thank you for your help,

      fintml

        • 1. Re: How to tell which document is active
          try67 MVP & Adobe Community Professional

          The "this" object usually returns the currently active document, depending

          on the context it's used.

          There's an array property of the app object called activeDocs, but I think

          it's ordered by the order in which they were opened, not by which one is

          currently active.

          • 2. Re: How to tell which document is active
            gkaiseril MVP & Adobe Community Professional

            You can check the document's name or path. Since you are opening the documents, you could build an array of of the object name and title.

             

            var d = app.activeDocs;
            console.println("Active documents: " + d.length);
            for (var i=0; i < d.length; i++)
            console.println(i + ": " + d[i] + " - " + d[i].info.Title);

            • 3. Re: How to tell which document is active
              fintml Level 1

              try67:

              'this' returns the active document when the command is run from the console, but when I run it from the method that was set to repeat by 'app.setInterval' it always returns the document that called the app.setInterval-method (in my case doc A) regardless of the currently active document. You are correct that the array 'app.activeDocs' returns contains the active docs in the order they are opened, at least it did in all the cases when I tested it.

               

              GKaiseril:

              'info.Title' would be a great way to distinguish the documents if only there would be some way to get a handle on the doc object that currently has the focus i.e. is active.

               

               

              Maybe there isn't a way to achieve this, I have bumped into some other tasks that seem trivial but somehow are not supported, like when the user zooms into a document there seems to be no way to get the coordinates of the part of the document that is displayed on the screen. This would be really useful when creating custom controls that are displayed on screen all the time, like a "hovering" map key/legend.

               

               

              Thanks guys for replying anyway!

              • 4. Re: How to tell which document is active
                gkaiseril MVP & Adobe Community Professional

                If you have a list of the active documents and their titles and the title of the document that has focus, you souled be able to walk the list and match the document title. If you think you will have the same name, then use the path property instead of the document title.

                • 5. Re: How to tell which document is active
                  fintml Level 1

                  Yes that's true, but how can I get the title of the document that has the focus?

                   

                  I'm sorry I don't seem to be able to describe the situation clearly. What I would like to do is the following:

                   

                  Doc A runs the following command when opened:

                  global.repeater = app.setInterval("repeat();", 1000);

                  ---------------------------------------------------

                  function repeat()  {

                     var active_doc_title = ???

                  if (active_doc.info.Title == "A")

                      //DO X  

                  else

                      //DO Y

                  }

                  ---------------------------------------------------

                  ??? == How to get the title of the document that  has the focus? ('this.info.Title' doesn't work as described below)

                  ------------------------------------------------------------------------------------------ ------------------------------------------------------------------------------

                   

                  'this.info.Title' always returns the title of the document that runs the command.

                   

                  For example:

                   

                  Document A runs the following command when it is opened: global.repeater = app.setInterval("console.println(this.info.Title);", 1000);

                   

                  Now when I open the console it prints A's title repeatedly.

                   

                  Then I open document B, it receives focus and becomes active. The console (that is now displayed on top of document B) still continues to print A's title.

                   

                  -----------------------------------------------

                  • 6. Re: How to tell which document is active
                    tobias.wantzen

                    Sorry for reanimating – to fintml this is certainly not important any more. But for all users, stumpling across in search of a solution to this question (as I did):

                    var _doc = this;

                    Is the frontmost (aka »active«) document in scripting.

                    • 7. Re: How to tell which document is active
                      Thom Parker Level 3

                      app.doc will return the active doc

                      1 person found this helpful