5 Replies Latest reply on Mar 25, 2016 8:01 AM by KnightD

    Get the activeDocument's equivalent number from the app.documents.item(n) collection

    KnightD Level 1

      I've been working with a few sets of documents, where I've been cycling through the documents with:

       

      for(myCounter = app.documents.length; myCounter > 0; myCounter--){

        myDoc = app.documents.item(myCounter-1);

        //Do stuff

      }

       

      I'd like to be able to get the app.activeDocument's number within the app.documents.item() collection. This will enable me to set the counter in the previous code snippet so that I would be able to close all document tabs to the left of the current document as I cycle through them.

       

      P.S. If someone can point me to directions of how to indicate a code snippet in this forum, I'd be grateful.

      Thanks!

        • 1. Re: Get the activeDocument's equivalent number from the app.documents.item(n) collection
          Loic.Aigon Adobe Community Professional

          Hi,

          I won't be confident in relying on such indeces as the collection is updated at any document selection change.

          For what it's worth I wrote a long time ago a very simple extension to close docs (still working in latest CC).


          Capture d’écran 2016-03-15 à 17.21.34.png

          You can download it here.

          Free - Envoyez vos documents


          To install it, paste it whithin application /Plugins/Panels folder.


          HTH


          Loic

          www.ozalto.com

          • 2. Re: Get the activeDocument's equivalent number from the app.documents.item(n) collection
            Vamitul Level 4

            Iterate through the documents collection:


            for (var i=0;i<app.documents.length;i++){

              if(app.activeDocument==app.documents[i]){

               /// do nasty stuff here

            }

            }

            However, i don't think the documents collection is in any way linked to the document tab order.

            I don't think even the LayoutWindows collection is linked to the tabs order.

            • 3. Re: Get the activeDocument's equivalent number from the app.documents.item(n) collection
              KnightD Level 1

              Vamitul, you're onto something with the indices. I created 5 documents named A-E and tried the following code:

               

              var myDocName ="";

              for(myCounter = app.documents.length; myCounter > 0; myCounter--){

                      myDoc = app.documents.item(myCounter-1);

                      myDocName += "app.documents.item(" + (myCounter-1).toString() + ");" + myDoc.name + "\n";

              }

              alert(myDocName);

               

              The index seems to change to whichever order a document was last viewed, with the activeDocument always being app.documents.item(0).

              I'll check the code you've submitted soon and let you know what I come up with.

              Thanks!

              • 4. Re: Get the activeDocument's equivalent number from the app.documents.item(n) collection
                Laubender Adobe Community Professional & MVP

                KnightD wrote:

                 

                … P.S. If someone can point me to directions of how to indicate a code snippet in this forum, I'd be grateful.

                Thanks!

                 

                For formatting code as code here in the forums, use "Use advanced editor" in the upper right corner of your reply or create discussion field.

                Select the text you want to format as code and chose

                >> Syntax Highlighting > javascript

                 

                That's the best option for ExtendScript code, I think.

                 

                Now to the other problem:

                 

                With InDesign's implementation of ExtendScript, if there is a active document, it always goes by the index number of 0 of the app.documents collection (that's different with PhotoShop for example).

                An equivalent for app.documents[0] seems to be app.activeDocument, but that's not true.

                 

                When an open document is opened without showing a window EDIT: (and no other document is open) then there is no activeDocument, but one in the app.documents collection.

                Close all documents and run the following snippet to see for yourself:

                 

                app.documents.add(false); //default for the first argument is: [showingWindow: bool=true]
                alert(app.documents[0].name);
                try{
                alert(app.activeDocument.name);
                }catch(e){alert(e.message)};
                
                

                 

                 

                Uwe

                • 5. Re: Get the activeDocument's equivalent number from the app.documents.item(n) collection
                  KnightD Level 1

                  Thank you for the explanation on both issues. I'd try to find some work arounds using odd things like window position, but nothing has stuck just yet. Very interesting about the activeDocument case you cite. Good to know!