Skip to main content
Participating Frequently
June 22, 2024
Question

Javascript to delete last 4 bookmarks of every 10 bookmarks

  • June 22, 2024
  • 2 replies
  • 550 views

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.

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
June 22, 2024

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();
          }
      }
   }
}
Participating Frequently
June 23, 2024

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.

Bernd Alheit
Community Expert
Community Expert
June 22, 2024

Has the first bookmark childrens?

Participating Frequently
June 22, 2024

No, none of the bookmarks have any children.

Bernd Alheit
Community Expert
Community Expert
June 22, 2024

This explains the error message.