Set Name in OnAdd doesn't work when text is pasted from clipboard

I have a container with the following OnAdd action defined:

$Name=$Name(parent)+$ChildCount(parent);$Badge=“link”;$Color=“black”

The action works when I move a node (an existing node in this tbx document) into the container.

A part of the action (below) doesn’t work if I paste some text form the clipboard to create a new node in this container

$Name=$Name(parent)+$ChildCount(parent)

Is there another way to set the name of a newly created note to the name of the parent node plus childcount? MyText1, MyText2, …

So you want the child to show the word count of the parent at the time of adding the note. Or do you want the current count.

Serendipitously, I just wrote a demo for another user who wants to show word counts - see here (demo file and lots of relevant aTbRef links).

As to pasting text into a container and it not firing the container’s OnAdd, I can’t replicate that. Until I have steps that reproduce the error, sadly I can’t help on that.

1 Like

So you want the child to show the word count of the parent at the time of adding the note. Or do you want the current count.

I just want to have the same name as the parent node plus a number to make the name unique (to avoid identical names in one container, it doesn’t need to be the count)

As to pasting text into a container and it not firing the container’s OnAdd, I can’t replicate that. Until I have steps that reproduce the error, sadly I can’t help on that.

I just copy text (ctrl+c) and paste (ctrl+v) the text inside a container. Everything works fine but only the name doesn’t change. The name only changes when I cut (ctrl+x) and paste (ctrl+v) the node again in the container

So the children could be “note 1”, “note 2”, “note 3”. If I understand the constraint here is the sibling notes all have the same title ($Name) so we need to add something to make it unique.

In the latter context , you earlier solution, even if the code worked would fail as unless the container $text is altered between every note being added, $WordCount(parent) will not be adding a unique element to the rest of the new child’s $Name.

I still can’t replicate thus but I think I can guess the issue. You’re pasting some info into a container and letting the app name it. Separately, the OnAdd is trying to use the on-the-fly created $name and change it. This container OnAdd works, I think. I have a container ‘bin’ with this $OnAdd and with 2 existing children:

$Text = $Name;
$Name = $Name(parent)+" " +$SiblingOrder;

Now if I copy some text, e.g. “I think I can guess the issue”, and—with focus in the container, blibbd paste the clipboard, I get a new note:

Name: “bin 3” (this is new sibling #3)
Text: “I think I can guess the issue

A weakness in this is if you select note “bin 1” and paste you now have two notes called “bin 2”. But we can work around that:

$Text = $Name;
$Name = $Name(parent)+" " +($ChildCount(parent)+1);

Turns out, that at this point as the note’s been added, it’s already in the parent’s child count. so:

$Text = $Name;
$Name = $Name(parent)+" " +$ChildCount(parent);

does that help? :slight_smile:

Thanks for this, this works much better for the “auto increment” part.

But still, if I paste from clipboard its shows the following.

I still can’t replicate the case in red (above). I can only get that if no note is selected or the selection is outside ‘Test-container’. But, in those circumtances the new note is created in a different location, not our test container, and so the special OnAdd code is not used (as I’d expect). this also means that in outline view you can’t use this method for the first such child. Why? Because you can’t set focus on the non-existent child branch of a non-container (in a map though you can drill down to an empty child map and thus set focus.

In my test you’ll notice that with the test container selected and using ⇧+↩ to make a new child, i get the correct name but no text. For the next 2 cases I left the last sibling selected and blind pasted the string “The pattern requires configuration via its arguments.”, which for items #2 and #3 turns up as the $text of the note.

Please note, I’m not suggesting you are doing anything ‘wrong’, just that when using your description alone, I cannot reproduce the effect you show. So, we are doing something subtly different. FWIW, here’s my test doc: OnAdd-test.tbx (110.7 KB).

Using that TBX with and of the 3 siblings selected, you should get a 'Text-container 4" added. where it is inserted will depend on which sibling is selected. If no note in the document is selected, your clipboard text should form the $Name of a new note at root level as first sibling. If the container note “Testdrive” is selected then the paste uses the clipboard text to name a new sibling note to that container. If “Test-container” is selected then the paste uses the clipboard text to name a new sibling note to that container.

1 Like

Thanks Mark for your help, much appreciated.

It works in your test on my desktop but if I copy your container to my big document it doesn’t work.

So, I deleted everything in my original document and now it’s working.

Now I just need to figure out which of the container, templates or code snippets in my document is causing this strange behaviour.

I will experiment until I find it and will let you know - hope this helps for future requests.

1 Like

How strange, but glad it is now working for you.

I did take some time to test if it was an (occasional) timing issue where the $Name value created from the pasted text was set after the note was created and was thus overwriting the name set by the code.

Other scenario that might occur but which I did not investigate:

  • Agents acting on aliases of the notes created in the container.
  • There is other code in the container rule, or in its edict that affects the child notes.
  • The container has a $Sort value set. I think this one very unlikely, but here in case

Thanks for the advise, this I will check, »Agent acting« sounds like the direction to start investigation

1 Like

I think this might have been a minor typo in the OnAdd action — one small enough to be easily overlooked, but that interfered with the evaluation of the $Name clause. I tried this here, and it did work correctly.