Counting the number of outbound links from a note for a display expression

I have a list of notes in a container that have outbound links. I want to count them and put that number in a display expression. Ideally, I would like to build 2 expressions for different purposes

  1. Count the total number of outbound links
  2. Specify several linkTypes to count

Here is what I want:
NameOfNote, LinkCount:7

I suspect the operators needed would be “links” and “count” but I am unsure how to put this together.
How would I do this?

OK, $OutboundLinkCount, is the sum of $PlainLinkCount and $TextLinkCount, but $OutboundLinkCount excludes $WebLinkCount,

The corollary to $OutboundLinkCount is $InboundLinkCount noting that the latter doesn’t sub-count text-vs.-basic etc.

Prototype links. Any note can have zero or one links of link type ‘prototype’. These are always ignored in counts for self-evident reasons. Prototype assignment/inheritance is stored as a basic link although it is not otherwise considered as such, thus its omission from counts and link-related operators in modern Tinderbox.

Basic links and aliases. If doing counts using aliases, e.g. when using agents to find the notes needing counts, be aware that alias may have different basic links from their original (text links must be the same as both use the same text. The trick is to make sure if using operators liked linkedTo() that you check the original note’s data, not the aliases (unless the latter is your deliberate intent).

Filtering by link type. Here you need the link checking operators:

N.B. if working from aliases (see above) you’ll be wanting to use the ‘original’ variants of the operators.

Thus, to test if any original note using the ‘Event’ prototype has an outbound link of the ‘Project X’ link type the agent query would be:

$Prototype=="Event" & originalLinkedTo("*", "Project X"); 

Note: as a result of writing this answer I’ve made improvements/corrections to my notes on the 4 operators linked above. So if following the links, you might want to shift+reload to refresh the page and update any cached version.

Oops, missed a bit. Let’s extend that last example. We’ll save to a list variable all the relevant links and get its size:

var:list vList = collect(find($Prototype=="Event" & originalLinkedTo("*", "Project X")),$IDString);
var:number vCount = vList.count;

OK, but we want the link type, ‘Project X’ above to be a variable that we’ll iterate from a list. So we’ll read the latter and iterate it using a version of he above within the loop. Better we’ll make an outer looped list of note(s) we want to populate with this data. For the Display Expression we’ll call $MyString and into the later we’ll place our title+ count.

Code follows…

1 Like

First we collect a list of the note’s whose Display Expression we wish to set. For this experiment, we’ll assume it is all the children of the current note:

var:list vNameList = collect(children,$$IDString);

Next we’ll make the list of types to check. I’ll hard code a value here, as no test doc was supplied, but you’ll be able automate this as you so please.

var:list vTypeList = [Project X;Project Y;Project Z]; // note use of new list defining style

Now we iterate nested lists, in the form

   // set DE then...
      // compute $MyString value to hold a 3 counts
// Result is a $MyString value like "Some note, 4/3/0" where the numbers ar type-gated counts for each of the 3 types for which we'll test. 


   $DisplayExpression(aName) = "$MyString";
   var:list vDEValues = "";
      var:number vCount = 0; // properly initialise and zero count
      vCount = collect(find($Prototype=="Event" & originalLinkedTo("*", aType)),$IDString).count;
      vDEValues = VDEValues + vCount;
   $MyString(aName) = $Name(aName)+", "+vDEValues.format("/");

N.B. this code is not tested, as no specimen data is available and making this sort of data up is both time consuming and prone to not test the real data in a proper manner.

1 Like

Many Thanks Mark.
Your explanation and code to get my code started, as usual is an IMMENSE, help. As the saying goes,

“I could not have done it without you buddy.”