10 Replies Latest reply on Jul 5, 2018 9:39 AM by markp9652695

    InDesign CS6 JS: Oject is Invalid Error

    markp9652695 Level 1

      I am trying to delete two pages from a document (CS6), but I get an Object is invalid error on line 3. Please help me resolve the error.

       

      var source_doc = app.documents.item('Trust Admin_2.indd');

      var sourcePages = source_doc.pages.itemByRange(2,3);//pages 2-3

      sourcePages.remove();//Object is invalid error

        • 1. Re: InDesign CS6 JS: Oject is Invalid Error
          yadavs92328 Level 2

          Code is working just fine in my system.

          Can you share file or more information about that?

          • 2. Re: InDesign CS6 JS: Oject is Invalid Error
            Laubender Adobe Community Professional & MVP

            Hi Mark,

            itemByRange() is working with index numbers. Index starts with 0.

            So your code tries to remove pages 3 to 4. If you have no page 4 in your document the code will throw an error.

             

            Change your code to:

            var sourcePages = source_doc.pages.itemByRange(1,2);//pages 2-3
            

             

            In one line the whole action:

            app.documents[0].pages.itemByRange(1,2).remove();
            

             

            Regards,
            Uwe

            2 people found this helpful
            • 3. Re: InDesign CS6 JS: Oject is Invalid Error
              markp9652695 Level 1

              @yadavs92328: I tested a document with placeholder text and I got the same error. It's illegal for me to send the Trust Administration document to you. I can't find any file attachment link in this forum.

              • 4. Re: InDesign CS6 JS: Oject is Invalid Error
                markp9652695 Level 1

                Hi Uwe,

                 

                This code works:

                app.documents[0].pages.itemByRange(1,2).remove();

                 

                 

                The following result in errors:

                (1)

                var source_doc = app.documents.item('Trust Admin_2.indd');

                var sourcePages = source_doc.pages.itemByRange(1,2);//ERROR: object is invalid

                var pag=source_doc.pages;

                pag.remove(sourcePages);//

                 

                 

                (2)

                var source_doc = app.documents[0];

                var sourcePages = source_doc.pages.itemByRange(1,2);//pages 1-2

                var pag=source_doc.pages;

                pag.remove(sourcePages);//ERROR: pag.remove is not a function

                 

                Regards,

                Mark

                • 5. Re: InDesign CS6 JS: Oject is Invalid Error
                  Laubender Adobe Community Professional & MVP

                  Hi Mark,

                  remove() takes no argument.

                   

                  Just add it to your sourcePages variable:

                   

                  sourcePages.remove();
                  

                   

                  app.documents.item("Name") or app.documents.itemByName("Name")

                  should work as expected as long there are no two or more documents open with the same name.

                   

                  Regards,
                  Uwe

                  1 person found this helpful
                  • 6. Re: InDesign CS6 JS: Oject is Invalid Error
                    markp9652695 Level 1

                    Hi Uwe,

                     

                    I am still getting errors and I'm trying to resolve them. Question for  you.

                     

                    Pages is a collection and has no "remove" method. The Page object does have a "remove" method. Why does the following code work:

                    app.documents[0].pages.itemByRange(1,2).remove();

                     

                    Regards,

                    Mark

                    • 7. Re: InDesign CS6 JS: Oject is Invalid Error
                      markp9652695 Level 1

                      Hi Uwe,

                       

                      This works:

                      var source_doc = app.documents.item("test.indd");

                      var sourcePages = source_doc.pages.itemByRange(1,2);//Error: object is invalid

                      sourcePages.remove();

                       

                      Regards,

                      Mark

                      • 8. Re: InDesign CS6 JS: Oject is Invalid Error
                        himanshug12764438 Level 1

                        I can confirm that the "object is invalid" error comes when the page range is invalid.

                         

                        Thanks,

                        Himanshu

                        • 9. Re: InDesign CS6 JS: Oject is Invalid Error
                          [Jongware] Most Valuable Participant

                          itemByRange (and its close relative everyItem()) is quite the magical command. Marc Autret calls its description in the Help "naïve", and I agree with him:

                          Array of Page everyItem ()

                          Returns every Page in the collection.

                           

                          The return type says it's an array, but that is only because JavaScript lacks the correct term for its actual type. It is not an actual array but a (live!) collection of singular objects. A property change or a method that is applied, applies to every single item in turn – some go so far to call it "immediately", as it is very, very fast, compared to having JavaScript loop over it.

                           

                          So this

                           

                          p = app.activeDocument.pages.everyItem();
                          alert (p.length);
                          alert (p.constructor.name);
                          

                           

                          shows an error on line 2, because 'p' does not contain "Pages", it contains "Page", and a Page does not have a .length property. You can see it does if you run line 3; it identifies 'p' as "Page".

                           

                          See Marc's Indiscripts :: On ‘everyItem()’ – Part 1 and Indiscripts :: On ‘everyItem()’ – Part 2 for an in-depth discussion of this mechanism.

                           

                          It is extremely useful if you are an advanced scripter and know how to properly use this. (Even then you may get the occasional surprise, but at least you know where to look for troubleshooting. Just the other day I was working with a collection of paragraphs and was dismayed to find that tinkering with the text contents changed the actual text inside my file. That was because a collection will always keep its reference to the original objects.)

                           

                          You can avoid working with everyItem, itemByRange, itemByName, and related methods, by explicitly using loops on the original array. The only thing to watch out for is that modifying the original array will (logically) mess with your array order, so to remove pages 2 and 3 you would not write

                           

                          document.pages.item(2).remove();
                          document.pages.item(3).remove();
                          

                           

                          … because if page #2 is removed, that place gets filled by what previously was page #3. Either account for possible changes, or simply work back-to-front:

                           

                           

                          document.pages.item(3).remove();
                          document.pages.item(2).remove();
                          

                           

                          No problem. (Although it seems your original problem is that you are not accounting for arrays and item collections starting with '0', not with '1'.)

                          3 people found this helpful
                          • 10. Re: InDesign CS6 JS: Oject is Invalid Error
                            markp9652695 Level 1

                            Thank you, Himanshu, Jongware, Uwe and yadavs92328.