Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com)
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi
Tinderbox Users >> Agent, Actions, Rules & Automation >> Getting the first child of a specific note...
http://www.eastgate.com/Tinderbox/forum//YaBB.cgi?num=1452229290

Message started by Pat Maddox on Jan 8th, 2016, 12:01am

Title: Getting the first child of a specific note...
Post by Pat Maddox on Jan 8th, 2016, 12:01am

I have a top-level note called "days", so its path is "/days". I want an agent which finds the first child, and then its children.


Code:
first("/days") // gives me the first child


Now I'm trying to find that thing's children:


Code:
inside(first("/days")) // this isn't it...


update: I think I got it to work. We'll see if it holds up under use...


Code:
eval(parent, first("/days"))


To be honest, I just lucked my way into this. I don't understand the documentation for eval at all.

Title: Re: Getting the first child of a specific note...
Post by Mark Anderson on Jan 8th, 2016, 5:27am

Functions like first() test each note (path) for a match and return a positive/negative. They don't return a set of paths of matched items. Thus if you nest inside(first()), all inside sees is 'true' or false' - which it can't use.

Here we know inside() want's a single target container, and we know the first child is a singular object (be it a note or a container). So we make this our agent query:

inside($Path(child("/days")))

inside() wants a note title or path. We'll go for caution and use path as it's less ambiguous. The designator child("/days") returns a reference to the first child note of '/days' and we use that as an offset reference for the $Path attribute. This being a path, inside() can use it. Sorry for the long explanation but the way this stacks up is more obvious after the fact - it can be hard to guess.

Also, if we have a String attribute $Target and set the agent's $Target value to "/days" (without quotes) and set that as a key attribute for the agent, you can then make the agent re-useable just be altering $Target with this query:

inside($Path(child($Target(agent))))

If $Target is a key attribute for the agent it's easier to edit that then open the Inspector or agent pop-up and edit a hard-coded note name in the query.

The above was tested in v6.4.0.

Title: Re: Getting the first child of a specific note...
Post by Pat Maddox on Jan 8th, 2016, 11:31am

Thanks! Yeah I never would have guessed that :) Primarily because the documentation says:


Quote:
Designators don't allow offset references as with attributes. Thus $Name("Some note") works but child("Some note") doesn't. If an offset designator is needed use find() instead.


I think I understand your code though. But how do I reconcile it with the above-quoted documentation? I'm clearly misunderstanding that part.

Title: Re: Getting the first child of a specific note...
Post by Mark Bernstein on Jan 8th, 2016, 11:47am

I think you're quoting above from aTbRef, and while aTbRef is amazingly accurate and up-to-date, even Mark Anderson nods at times.

Problem: compose a query that is true if a note is a child of the first child of /days.

Solution:  The function inside() is true for notes which are children of that note.  For example, inside(/days) would find all the children of the top-level container named "days".

Now, how can we describe the container we're interested in?  That container is the first child of /days.  So

    inside(child(/days))

should give us the result we want.  I've tried this on a sample file and it appears to behave as Id expect.

- - -
Exercises for the reader

1. Find the notes inside the last child of /days, not the first.
2. Find the notes inside the ''second'' child of /days.

Title: Re: Getting the first child of a specific note...
Post by Mark Anderson on Jan 9th, 2016, 10:31am

@Pat, if you find something in aTbRef that you think is wrong (including doesn't work as described) or out of date, or ambiguous etc, do please drop me line. aTbRef is a public document written /maintained by me and not a formal Eastgate document. If I know them, most errors can be fixed pretty instantly. Also, reading by a fresh pair of eyes often shows up hitherto unseen ambiguity in the writing.

As regards designators it transpires the comment there re offset references is out of date. It's not clear when. It was true when I wrote it, but undocumentent changes around the time of 5.12.x likely made the info obsolete. I'll be removing it in a forthcoming update.

I repeat, for anyone reading this, that I do welcome reports of errors/typos/etc.!  :)

Title: Re: Getting the first child of a specific note...
Post by Mark Anderson on Jan 9th, 2016, 11:37am

I've updated all the online aTbRef pages on designators to remove the out of date info (as revealed by discussion above).

Tinderbox User-to-User Forum (for formal tech support please email: info@eastgate.com) » Powered by YaBB 2.2.1!
YaBB © 2000-2008. All Rights Reserved.