Create note sibling with action code?

I want to create through action code a note that is the sibling (i.e. on the same level) as the note from which the action code runs. But I am a bit at a loss how to do it.

Say I have a note that is at /Root/container1/container2/note_in_question.
I know that the intrinsic variable $Path holds the path of the note including its name. So my idea is that all I need is the path without the note’s name (let’s call that $RestPath), for then I can create the new note as $RestPath + $NewNoteName.

But how do I get $RestPath? One way I tried is to split $Path:

$MyString=$Path.split("/");

This gives me the elements of the path in $MyString, separated by semicolons.
Then I could assemble the new path through

$MyPath="/" + $MyString.first + "/Draft";

But this obviously works only one level “down” - if the note is further down, I don’t know how to do it. And that’s why I would be grateful for any input and ideas!

I see several ways to do this.

First, you could use the two-argument version of create:

create("new note",$Path(parent))

Of course, you could also set $MyString=$Path(parent) in the approach above.


That solves the immediate problem, but you might want to use the path manipulation approach described above.

As you saw, you can use split to get the components of the path. I’d use a list:

$MyList=$Path.split('/');

Now, you can lop off the last element:

$MyList=$MyList.first($MyList.count-1);

Now you can reassemble the list to a path:

$MyPath=$MyList.format('/');

Many thanks - this sounds like a very good way to try.
Thanks for lending me a hand in the code jungle that much of this still is for me. While I can understand most of what others write with the help of things like aTBRef, it is much more difficult to come up with solutions myself, I realise.

1 Like