Creating a "generic" agent to count notes with attribute various values

Hello,
My context is this: I have a script with scenes and each scene has a location.

I want to get a status of all occurrences of all locations:
3 scenes occur in Loc A, 2 scenes occur in Loc B…

Currently I do that “manually” by creating one agent per Location:
Agent Loc A = descendedFrom(“/Scenes”)&$MyLocation.contains(“Loc A”);
Agent Loc B = descendedFrom(“/Scenes”)&$MyLocation.contains(“Loc B”);

These agents are in a container so I can plot them or sort their displayValue by childCount.

It is not that painful but I don’t know all of my locations yet and I could need this process for attributes with many more values.

I tried the following code - it does not work and does not feel right as it should first create a Note with the Location Name then link the necessary notes in it.

Then again what would happen to those notes on the next run if, for example, a location were renamed?

(db enclosed).
TestLocations.tbx (103.1 KB)

if(descendedFrom(“/Scenes”)&!$MyLocation.empty;) {
var: string res = $MyLocation;
$MyLocation.contains(res); //
}

Any pointer most welcome.
Regards,
Philippe