Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Searching in a Treeview (scripting)

  • Searching in a Treeview (scripting)

    Posted by Neil Redman on February 8, 2013 at 3:44 am

    Hi, I am trying to create a dynamic treeview, that can have several nodes and each of those can several childnodes deep.

    I want to be able to search the entire tree for a specific string. So say I want to see if the word “test” is already the name of one of the nodes in the tree.

    How would I do that?! I’ve been trying different things for 2 days now without much success. I tried to add all the names to an array, but don’t know how to get all of them.

    I was doing something like (PseudoCode):
    add Tree.items to Array
    add Tree.items[0].items to Array
    add Tree.items[0].items[0].items to Array
    add Tree.items[0].items[1].items to Array

    Since I don’t know how complex the tree is in any given case, this becomes complex very quickly. I’ve tried various loops but without any success.

    I hope someone has some ideas, how to work with this!

    Thanks,
    Neil

    Mitch Mann replied 13 years, 3 months ago 2 Members · 3 Replies
  • 3 Replies
  • Mitch Mann

    February 9, 2013 at 5:17 pm

    Check out “Script UI for Dummies” for a way to do a search in a list box

    https://www.kahrel.plus.com/indesign/scriptui-2-0.pdf

    It might inspire a solution, but I don’t think it specifically addresses a search in a tree view. Let us know if you figure out a way to do it, I’d be interested in knowing!

  • Neil Redman

    February 11, 2013 at 5:04 am

    Hey, thanks for the link, I already found that document and had found out a way how to do it. The main idea behind it was to have a function call itself. It is similar to what they do on page 45 of the document when they are expanding all the nodes in a treeview. I pushed all the elements to an array and then simply searched the array.
    To search the array I used indexOf which is by default not enabled in After Effects Scripting because it uses an older JavaScript version. On this site (https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/indexOf) they give you some code, that you can add to your script to enable the indexOf method of arrays.
    This is the code I used to push the treeview items to the array:
    (Tree is the name of my Treeview and names the array)

    function Names() {
    names=[];
    for (var i = 0; i < Tree.items.length; i++)
    {names.push(Tree.items[i].text)}
    for (var i = 0; i < Tree.children.length; i++)
    {getNames (Tree.children[i]);}

    function getNames (node) {
    var kids = node.items;
    try{
    for (var i = 0; i < kids.length; i++){
    names.push(kids[i].text)
    if (kids[i].type == “node”) getNames (kids[i])};
    }
    catch(err){}
    }

    I had to add “try” and “catch” to the code, because it appears to produce errors when they are no items left. Hope this helps 😉

  • Mitch Mann

    February 13, 2013 at 2:24 am

    Cool! Makes me think of building a media browser for AE, like premiere has. What are you using tree view search for?

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy