How does eachLink(LoopVar){} work?

Does someone have an example of how to make this work? eachLink(LoopVar){}? When would you use it?

eachLink(x){…}
Examines each link, either inbound or outbound to this note, with the exception of prototype links. The local variable x is bound to a dictionary of link properties, include source, destination, type, class, title, target, url, comment, and anchor. Changes to this variables values are not (yet) recorded as changes to the link.

For example, does this note have a link of type “agree”?

function isAgreeable() {
     eachLink(x) {
        if(x.type=="agree") {return true;}
        };
     return false;
     }

For example, count the number of links from this note to tasks:

function linkedTasks() {
    var:number count=0;
    eachLink(x) {
        if($Prototype(x.destination)=="Task") {count += 1;}
        };
    return count;
1 Like

Thanks. @mwra, this would be a nice example for aTbRef.

@satikusala’s suggestion actioned, see my article on eachLink(LoopVar).

The concept of local variable, in this case, is not clear. How can I access eachLink(aLink) for a given note? That is, something like 'note'.eachLink(aLink){}; How can (a.) I feed a Path or ID as arg in this case, or (b.) let tbx know the correct note, or (c.) parse all the links in the doc using action code?

eachLink() iterates the links of the current note. How, in code, might the current focus change? You can use a loop and run eachLink() inside the loop. The latter might be items in a list of notes, or even $IDS (as $IDs can act as proxy for $Path). So (code not tested as busy right now)

$MyList.collect(children,$ID);
$MyList.each(anID){
   eachLink(aLink,anID){
      // do stuff on the links of the note defined by current anID's $ID
   }
}

To parse all the links, ensure the equivalent of your $MyList is a list of the IDs of all the notes whose links you wish to process.

Does that help?

[added a missing parenthesis and the second argument to eachLink, Nov 9 10:50am MB]
[note: revised syntax is not (yet!) in Tinderbox as at v9.3.0]

Another route is to let each note call eachLink() via an edict.

If you’ve a big doc and lots of links, i suspect this may take more than an instant, whichever method you use.

In case there’s a different approach we might use, what is the reason to check every link of every (content?) note?

Mark @mwra, I tried this without success. Tinderbox does not correctly identify the current note. Maybe @eastgate will some insight into the matter.

eachLink has two variants. eachLink(x,theNote){} examines each link of theNote. eachLink(x) examines each link of this, and is equivalent to eachLink(x, this).

In a rule, this is bound to the note that owns the rule. In an agent action, this is the note that satisfied the agent’s query. In a stamp, this is the selected note.

2 Likes

<cough> checking my notes the optional extra argument (i.e. theNote above) is in beta only and not (yet) released publicly†. This is also why it’s not covered in the current aTbRef.

†. Backstage users will be able to use the new syntax if they have b568+.

3 Likes

Oops!