• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Javascript to delete last 4 bookmarks of every 10 bookmarks

Community Beginner ,
Jun 22, 2024 Jun 22, 2024

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.

TOPICS
JavaScript

Views

171

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2024 Jun 22, 2024

Copy link to clipboard

Copied

Has the first bookmark childrens?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 22, 2024 Jun 22, 2024

Copy link to clipboard

Copied

No, none of the bookmarks have any children.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2024 Jun 22, 2024

Copy link to clipboard

Copied

This explains the error message.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 22, 2024 Jun 22, 2024

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();
          }
      }
   }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 23, 2024 Jun 23, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines