Tinderbox Forum

New v8 Scripting features

Admin note: split this to a new thread given divergence from original topic. Move is no relfection on the content itself - which is most interesting!

And of course, with JavaScript …

What seems to be the best route to listing the children of a note through the new osascript API ?

(my first thought is the read the childCount and ID attributes, as below, but I may well be missing something simpler)

-- noteChildren :: Note -> [Note]
on noteChildren(x)
    using terms from application "Tinderbox 8"
        if 0 < value of attribute "childCount" of x then
            set v to child of x
            set strID to value of attribute "ID" of v
            set lastID to value of attribute "ID" of lastChild of x
            
            set xs to {v}
            repeat while lastID ≠ strID
                set v to nextSibling of v
                set strID to value of attribute "ID" of v
                set end of xs to v
            end repeat
            return xs
        else
            {}
        end if
    end using terms from
end noteChildren

Forgive me – I was being unclear, I was thinking not of GUI interactions (selection is indeed there, as a property of windows) but of scripted traversals of a sub-tree, given a particular note.

I’m not entirely sure why you’d want to traverse the tree in a script, when it’s so much faster to do it inside Tinderbox. Yet not doubt there are reasons!

Note that children gives you a list of immediate descendants; that should make preorder and postorder traversals straightforward.

If people need to do this often, we could expose the next and sentinel properties of each note.

Note that children gives you a list of immediate descendants

Curiously enough there’s no mention of a children method in the dictionary, and as far as I can see note objects don’t respond to an attempt to call a method of that name.

The only immediately visible methods for child access seem to be child , nextSibling and lastSibling (in the style of an XML interface), so for the moment I’m having to build a slowish chain of nextSibling links.

(Re your other question, traversals etc allow me to generate nested diagrams from my outlines in graphics packages, I use a hub Tree structure, and I have various toTree and fromTree translators in different languages and for different apps)

Ah ! got it.

There is indeed something analogous to a children method, which I had failed to discern in the dictionary.

In Applescript if we have bound the the name oNote to a parent node, we can obtain all its children, at the cost of just one apple event, by writing notes of oNote.

In JS, we obtain them with dot method syntax: oNote.notes()

This means that the whole outline (or the sub-tree beneath a selected node) can be read into a generic Tree structure in JS with:

// pureTreeTBX :: Note  -> Tree Note
const pureTreeTBX = note => {
    const go = x =>
        Node(x, x.notes().map(go));
    return go(note);
};

and in AppleScript with:

-- pureTreeTBX :: Note -> Tree Note
on pureTreeTBX(oNote)
    using terms from application "Tinderbox 8"
        script go
            on |λ|(x)
                Node(x, my map(go, notes of x))
            end |λ|
        end script
        |λ|(oNote) of go
    end using terms from
end pureTreeTBX

Sorry! One hazard of being the developer is that I sometimes remember earlier versions of the software.

The property notes returns the immediate children of a note.

1 Like