Skip to main content
subieguy2
Inspiring
May 1, 2019 12:14 PM UTC
Answered

Search for bookmark

How can I search through my list of bookmarks and assign a JavaScript action to the one I am looking for?

If I run the following script:

for (var i = 0; i < this.bookmarkRoot.children.length; i++){

console.println(this.bookmarkRoot.children.name);

}

I get the top level of my bookmarks but not the bookmarks nested under these.

Console shows:

Schematic Tools - Volume 1

Schematic Tools - Volume 2

Here is what my bookmarks look like:

I am needing to look for specific bookmark names and assign different JavaScript functions to them.

So something like this is what I am looking for:

for (var i = 0; i < this.bookmarkRoot.children.length; i++){

if (this.bookmarkRoot.children.name == "V1 - 021 (Orange)"){

cExpr: "oneTwoOne();"

}

if (this.bookmarkRoot.children.name == "V2 - 032 (Red)"){

cExpr: "zeroThreeTwo();"

}

}

How do I search for the nested bookmark and assign it my desired function?

Any tips would be appreciated!

This topic is closed to new replies. Start a new post to keep the conversation going.
Bernd Alheit
Bernd AlheitCorrect answer
Community Expert
May 1, 2019 1:30 PM UTC

Look at the children of the children:

for (var i = 0; i < this.bookmarkRoot.children.length; i++) {

  var bk = this.bookmarkRoot.children;

  for (var j = 0; j < bk.children.length; j++) {

    console.println(bk.children.name);

  }

}

9 replies

Legend
May 1, 2019 2:46 PM UTC

You need to add a line to check that bk is defined, since you have top level bookmarks without children

Translate
Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
May 1, 2019 1:30 PM UTC

Look at the children of the children:

for (var i = 0; i < this.bookmarkRoot.children.length; i++) {

  var bk = this.bookmarkRoot.children;

  for (var j = 0; j < bk.children.length; j++) {

    console.println(bk.children.name);

  }

}

Translate
Legend
May 1, 2019 1:44 PM UTC

And if the children may have children you are going to need to use recursion. A standard programming technique where a routine calls itself. There are alternatives but they are very messy.

Translate