Pruning trees...of the data structure variety.

I hope someone can follow this, cause it’s driving me nuts. I’ve stripped the problem down to the essentials:

  1. Here is my node script…very simple. Each node contains a reference (to a button in interface bulder) and a list of child nodes:

on node(theObjectReference, theList)
  script node
    property theParent : theObjectReference
    property theChildren : theList
  end script
  return node
end node

  1. I have hard coded my tree. It’s big. It has a lot of object references (ie. button “myButton” of tab view item “myTab” of tab view “myTabView” of window “main”). They are all correct…I tested each one individually.

  2. I have a global called root, and a function called initTree(). This function is where the global variable is initialized to my hard coded tree.

  3. I have a search function. It’s job it to find the matching object reference (when a button is clicked) and return the list of its children.


on searchTree(theButton, root)
  set children to theChildren of root
  --make comparison
  if theParent of root is equal to theButton then
    --found it
    return children
  else
    --loop over all children
    repeat with child in children
      --recursive search call
      set foundChildren to my searchTree(theButton, child)
      --if it was found in child, propogate it up the stack
      if foundChildren is not equal to {} then
        return foundChildren
      end if
    end repeat
  end if
  --not found, return null by default
  return {}
end searchTree

  1. The problem. It doen’t work…quite. A few things to know:
  • some things I search for are found correctly always
  • the first thing I search is found always, every time I search for it
  • some subsequent searches for other items are not successful
  • (ex. if I search for A, then B, it will find A but not B. If I run it again and search for B, then A, it will find B but not A. In both cases it will find C)
  1. If I call initTree() before I search each time, then the search ALWAYS works, for EVERYTHING I search for, so the tree/function are right at the start.

  2. So what the heck? Am I mutating the tree by calling search? I think not, because if I print out a label for each node I visit, the tree is the same each time, and every node is always in the right place. Nonetheless, the REFERENCE stored in the node seems to fail. If I try the line of code


log title of (theParent of the child) as text

to print the title of the button referenced by each node in the tree, the references that aren’t found by the tree also yield a “NSCannotCreateScriptCommandError (10).” This, of course, despite the fact that if I hard code this statement for every reference in my initial tree, it works.

HELP! (please and thank you) I’m going nuts on this one.

OK. I believe I know WHY it is happening now…which was the hard part for anyone else to figure out…with all that code. Hopefully now that I know why you guys can suggest the best workaround.

I have a tab view with 3 tabs. It turns out, I can ALWAYS find what I search for in 2 places:

  1. Any button that lies directly on the main window
  2. Any button within the tab that is selected when I call initTree()

In other words, since the other tabs aren’t visible when I call it, the references don’t actually get stored in the tree, even though the references themselves are right.

(ie. search A is for anything on seleected tab, search B is anything on the other tabs, and search C is the stuff on the main window.

SO…

Any thoughts? Should I call initTree() whenever a new tab is selected? Should I make a separate tree for each tab, and condition on the tab selected before searching? Is there a bettter way…?

Thanks guys! I appreciate it.