Copy link to clipboard
Copied
I have a document that could be 20-100 pages long and I want to delete the pages that I have in an array. When I run this I get crazy results. It deletes pages 8, 10, 12, 14, 19. I don't get that result. Any help would be appriciated.
var myDoc = app.activeDocument;
delArray = [7,8,9,10,14,17];
for (i = 0; i < delArray.length; i++) {
myDoc.pages.item(delArray[i]).remove(); //Also throws error 'object is invalid'
}
You could try a reverse loop thru the pages, check if the page in the array and then delete , something like this:
var p = app.activeDocument.pages;
delArray = [7,8,9,10,14,17];
for(var i = p.length-1; i >= 0; i--){
if (checkItem(delArray, i+1)) {
p[i].remove();
}
}
/**
* Checks if an item is in an array
* the array to check
* the item to look for
* true if the item is in the array
*
*/
function checkItem(a, obj) {
for (var i = 0; i < a.length; i++) {
...
Copy link to clipboard
Copied
First sort the array & delete those pages in reverse order...
delArray.sort();
for(var i = delArray.length-1; i >= 0; i--){
if(myDoc.pages.item(delArray[i]).isValid){
myDoc.pages.item(delArray [i]).remove();
}
}
Sunil
Copy link to clipboard
Copied
So close, it deletes, 8, 9, 10, 14, 18. It gets the center of the array but not the first and last.
Copy link to clipboard
Copied
You could try a reverse loop thru the pages, check if the page in the array and then delete , something like this:
var p = app.activeDocument.pages;
delArray = [7,8,9,10,14,17];
for(var i = p.length-1; i >= 0; i--){
if (checkItem(delArray, i+1)) {
p[i].remove();
}
}
/**
* Checks if an item is in an array
* the array to check
* the item to look for
* true if the item is in the array
*
*/
function checkItem(a, obj) {
for (var i = 0; i < a.length; i++) {
if (a[i] === obj) {
return true;
}
}
return false;
}
Copy link to clipboard
Copied
That did it, thanks so much!