Copy link to clipboard
Copied
Hi
I have cobbled up some javascript to delete last 4 bookmarks of every 10 bookmarks but it gives me an error saying "TypeError: bm.children[0].children is null
13:Folder-Level:App:Remove last 4 of every 10 bookmarks.js"
Here is the script
if (app.viewerVersion < 10) {
app.addMenuItem({ cName: "RemBM last4every10", cUser: "RemBM last4every10", cParent: "Tools",
cExec: "Removelast4ofevery10BM(this)", cEnable: "event.rc = (event.target != null);"});
} else {
app.addToolButton({ cName: "RemBM last4every10", cLabel: "RemBM last4every10", cTooltext: "RemBM last4every10",
cExec: "Removelast4ofevery10BM(this)", cEnable: "event.rc = (event.target != null);"});
}
/* Remove last 4 of every 10 BM in the document */
function Removelast4ofevery10BM(doc) {
console.clear(); console.show();
var bm = this.bookmarkRoot;
var bmlength = bm.children[0].children[0].children.length;
try {
for (var i = 0; i < bmlength; i++) {
if (i % 10 >= 6) { // Remove the last 4 bookmarks out of every 10
bm.children[0].children[0].children[i].remove();
}
}
}
catch(e) {
app.alert("Processing error: "+e)
}
}
Can someone help please. I am a total novice with javascript. It would be greatly appreciated.
Copy link to clipboard
Copied
Has the first bookmark childrens?
Copy link to clipboard
Copied
No, none of the bookmarks have any children.
Copy link to clipboard
Copied
This explains the error message.
Copy link to clipboard
Copied
Beside the issue with the non-existing children bookmarks you're trying to access, there are several other issues with your code. Try this:
Removelast4ofevery10BM(this);
function Removelast4ofevery10BM(doc) {
var bm = doc.bookmarkRoot;
if (bm!=null) {
for (var i = bm.children.length-1; i>0; i--) {
if (((i+1)%10)==0) { // Remove the last 4 bookmarks out of every 10
bm.children[i].remove();
bm.children[i-1].remove();
bm.children[i-2].remove();
bm.children[i-3].remove();
}
}
}
}
Copy link to clipboard
Copied
Thank you try67. Your code works brilliantly. The code I had created was from chatGPT so it has a very long way to go I guess. Thank you one again.