Qualifying a Display Expression

I have a container with 6 current. tasks that I have to complete in setting up the plot for a novel. Three of them have already been completed and have a “check mark” badge. I want the Display Expression on the Container to show how many tasks there are yet to complete.

This is the Display Expression I’ve been using, but it still returns 6 (the total number of tasks):

$Name+" : "+sum_if(children,$Badge!==“check mark”,1)

What am I doing wrong? Or is there a better way to do this?

Thanks

$Badge !== “check mark”

The operator should be !=. We use == for equality because a single = means assignment. But != is unambiguous.

I’m not wild about doing work in $DisplayExpressions. Display Expressions are lazy; they can be invoked anytime Tinderbox wants to display something, and they get cranky if they’re asked to do a lot of work. (This isn’t a lot of work, so it’s fine in practice. Just saying…)

The way I might do this is to use a rule on the container.

$MyNumber = sum_if(children,$Badge!==“check mark”,1)

Then the display expression can be simply

$Name+": "+$MyNumber

1 Like

HI,

I tried what you said; however the rule is still returning 6 (the total number of tasks) for the value $MyNumber. I want it to return 3 (the number of tasks withou the Badge “check mark.” So does this rule in words say, "Set the value of $MyNumber by adding together all children of this container whose Badge is not “check mark” ?

I tried with both: $MyNumber = sum_if(children,$Badge!=“check mark”,1)

and with: $MyNumber = sum_if(children,$Badge!==“check mark”,1) [!==]

Both return 6, instead of 3.

I fixed it by putting the negation in front:

$MyNumber=sum_if(children,!$Badge==“check mark”,1);

Is that kosher?

It did work, and I tested by adding and removing checkmarks and got correct number.

Yes, that’s fine. As a matter of style, I prefer

!($Badge==“check mark”)

Also, beware of curly quotes – you wasn’t straight quotes in your actions!

I’m trying to figure out how to do something similar, but a little different. Basically, I want to create a display expression that will just show the number of notes in a parent container (folder) but not include the other child containers & agents (again, just the number of notes). How would one write that kind of qualified display expression?

Thanks!

OK, let’s say we have a set like this

Projects
     Moon Landing
     Mars Colonization
     Shopping
            Groceries
            Spices
     Agent

Moon Landing wants to know how many notes are inside Projects, not counting containers and agents.

We can get a list of all the children of Projects easily enough:

collect(children(parent),$Path)

We only want some of these children — the ones that are not agents or containers.

collect_if(children(parent), (!$IsAgent) & ($Childcount==0),   $Path)

And we only need the number of items in this list:

collect_if(children(parent), (!$IsAgent) & ($Childcount==0),   $Path).count

Not tested: beware of typos!


Admin edit
(as later readers were confused by mention of an ‘IsAgent’ attribute)

There is no system attribute IsAgent as suggested in the pseudocode above. The normal test for an agent is the query term:

$AgentQuery!=""

In the case above, were we wish to filter out agents, i.e. not find agent objects, the query term is:

$AgentQuery==""

Amazing. Thank you for spelling all of this out…

So, when you say…

…that means, if I understand you correctly, in that instance one is including both the children (notes) and also the agents / containers, correct?

Thank you, again…

The collect_if makes a list of all our siblings that satisfy the following condition:

(!$IsAgent) & ($Childcount==0)

Agents won’s satisfy the first clause, and containers won’t satisfy the second clause. So we’ll have a list that only contains notes that are neither agents or containers.

Ok, thanks for your reply. I’m sorry, but I’m grasping how these are different in terms of what they display…

collect_if(children(parent), (!$IsAgent) & ($Childcount==0),   $Path)

collect_if(children(parent), (!$IsAgent) & ($Childcount==0),   $Path).count

Also, would I need to add or amend these if I want to exclude any aliases? Thank you for your help!

The first makes a list of eligible notes. The second takes that list and passes it to count, which counts it.

1 Like

Got it. Thank you! I’m beginning to get the hang of this, thanks to everyone’s wonderful help…

Hi again,
So I actually tried copying and pasting this into the Display Expression window:

collect_if(children(parent), (!$IsAgent) & ($Childcount==0), $Path).count

As you’d expect, it didn’t work. Looking at the code more closely, it looks like this is set up for an agent. I should’ve been more clear: I was hoping to apply this to a container (folder) that contains agents.

Is there some kind of code for display expression that I can use that would accomplish what I’m seeking for displaying the count for notes, but not agents agents / containers and aliases?

Thank you!

You want to do this in a display expression for a container that holds notes?

Let’s start simple:

$Name + ": "+$Childcount

Thank you. I tried that, but it didn’t display anything; no number in brackets.

For discrete agents, I’ve used…

$Name+" ["+$ChildCount+"]"

But that code seems to produce a count that also includes agents / containers and aliases.

Worked fine for me. Sorry

That’s odd. I’ll keep tinkering to see if it’ll work…

As a general principle, it helps a lot to keep processing simple in display expressions. Display Expressions are evaluated frequently, and so it helps to keep them fast.

So, in general, if you are inclined to want a complex display expression, consider splitting the work in two. Let a rule or edict do the heavy lifting, storing their results in a string attribute such as $MyString. Then the $DisplayExpression can be very simple:

$Name+": "+$MyString

This also helps diagnose problems, because now you can separate the computation behind $MyString. If that’s causing trouble, try simplifying it until it can’t possibly be wrong. Then, step by step, add in complexity until you get where you want to be.

1 Like