Code is working just fine in my system.
Can you share file or more information about that?
2 people found this helpful
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:
@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.
This code works:
The following result in errors:
var source_doc = app.documents.item('Trust Admin_2.indd');
var sourcePages = source_doc.pages.itemByRange(1,2);//ERROR: object is invalid
var source_doc = app.documents;
var sourcePages = source_doc.pages.itemByRange(1,2);//pages 1-2
pag.remove(sourcePages);//ERROR: pag.remove is not a function
1 person found this helpful
remove() takes no argument.
Just add it to your sourcePages variable:
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.
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:
var source_doc = app.documents.item("test.indd");
var sourcePages = source_doc.pages.itemByRange(1,2);//Error: object is invalid
I can confirm that the "object is invalid" error comes when the page range is invalid.
3 people found this helpful
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.
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".
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
… 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:
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'.)
Thank you, Himanshu, Jongware, Uwe and yadavs92328.