As you can see, this command will require a text object, an index and the text of the endnote. The text object will be the document text, the index will be the index of the note reference, and the text of the endnote will be "subtext" of the document text that is in the range between the corresponding In note reference numbers. So all the necessary information will be known once you find the numbers. You can do that like this:

The macro find command will return (text) selections. In this case, since it's a find all command it will return an array of such selections. A selection, as we've discussed before, is a pair consisting of a text object and a range. The text object in this case should always be the document text and the range tells you where in the document the number is located.
You say that the only numbers in your document are the reference numbers. So if you have 5 notes, that should return 10 selections; 5 for the note references, and another 5 for the in-note references. You can use the former as insertion points for your endnotes and the latter to identify the endnote text bits. Split the array in two, which should now match up. Loop through one and use the other for the matching info.

I'll attach a macro that implements this, and works for the simple case that I tested it on.

Aha Philip beat me to it! But I was writing up a macro before I saw his post so I'll post my macro as well.

It also looks like Philip's macro has some errors. I tried it on Nobumi's sample text and it failed. That's probably because Philip's macro tries to be more efficient in exactly calculating all the notes upfront. That process is going to be faster, but more brittle.

It also looks like Philip's macro has some errors. I tried it on Nobumi's sample text and it failed. That's probably because Philip's macro tries to be more efficient in exactly calculating all the notes upfront. That process is going to be faster, but more brittle.

I did only one quick test on Nobumi's text and it worked for me, but no question that the approach is brittle.

So here's another approach. This uses the fact that with Nisus' Find/Replace you can delete stuff and still get an array of selections/locations. But you still need to work backwards inserting the notes.

Here I gather the note texts first and then delete them. Then I find the note refs (and delete them) and finally insert the saved note texts at the correct locations.

I tested them on my problematic file. None of them worked the first time, but I could get Philip's last macro work with a slight modification.

I think the main problem was that the example that I gave in my first post was not good -- not sufficiently explained. I think the first macro by Philip assumes that all digits in the text are note references, either in the main text or in the notes (the sixth line has: "$numSels = $text.find '\d+', 'Ea':). But my file has many other digits either in the main text or in the notes... Anyway, it stops at the tenth line: "$noteRefInNoteSels = $numSels.subarrayAtIndex($noteCount,$noteCount)", with the error "The given index (864) is out of bounds for the array (count 863)".

Martin's macro runs but ends with this error message: "Converted 0 notes but had 539 errors! First Error: Could not find a matching note for number: 1" I don't understand the number "539" -- and I don't understand the meaning of the find command in line 27: "Find Next @String<(?<!\d|^)\d+>, 'E¢-W'" -- but anyway, it failed on my file.

Finally, I could get the last macro by Philip by modifying the find command "$noteSels = $text.find '^\d+ .+(?:\n[^\d].*)*', 'Ea'" to "$noteSels = $text.find '^\d+\s*.+(?:\n[^\d].*)*', 'Ea'", because each note reference in note was followed not by a space but by a tab...

Hello Nobumi,
I'm glad you got it to work. So I guess it was worth providing more than one macro! Also it shows that with macros it's really best not just to hope someone can write one for you, but to at least have some idea how they work so you can get the result you want.

Yeah, I guess that's kind of cryptic. This is of course one of the crucial bits of Martin's macro, since it finds the Note reference numbers in the text.

First the @String is the newfangled way to write string literals in Nisus Macro language. It means that the following stuff in whatever brackets is to be taken as a string. The crucial bit is the whatever brackets. Basically the character right after the @String forms the opening bracket and the closing bracket is the next matching same character, except that for some characters like "<" the matching bracket will be the opposite facing one. So here the closing bracket is the following ">". So this makes the find string: '(?<!\d|^)\d+'. The easy part is the '\d+', which finds the number. So what's the '(?<!\d|^)' ? Here the complicated parenthesis is '(?< … )' This is the preceded by parenthesis. inside that is the following '!\d|^' The pipe '|' is the OR symbol, and the exclamation point '!' is the NOT symbol. So the whole thing reads as: "Find a number that is preceded by either a non-digit or a paragraph start."

Finally the last special thing is the '¢' option. That allows the macro to later use the found bits from the expression as variables. You'll see in the code that a few lines later, Martin uses a variable '$0'. That refers to the found bit of this expression, i.e., the putative footnote/endnote reference number.

Hello Philip,
Thank you for your reply and your lesson. Thanks to it, I could understand the cryptic find pattern. I tried the formula '(?<!\d|^)\d+' on my document and could find that it matches all the reference numbers in the notes text. But then I don't understand the find pattern at the line 33: '^$0(.+$0\n)+'; if '$0' stands for the found reference number, the first one should be '^1(.+1\n)+'. But such a formula does not match any string... Martin comments for this part of the macro "# find the corresponding note text". If I understand well, that should match the first note text, which may or not contain several lines... So anyway, I am lost.

On the other hand, I realized that in my first post, I stated that 'Fortunately, there is no digit in the original text other than note references'. This was not true at all! I am very sorry to induce you to an error...

Anyway, I could get your last macro work, so I am satisfied, and I thank you very much again.

But then I don't understand the find pattern at the line 33: '^$0(.+$0\n)+'; if '$0' stands for the found reference number, the first one should be '^1(.+1\n)+'. But such a formula does not match any string... Martin comments for this part of the macro "# find the corresponding note text". If I understand well, that should match the first note text, which may or not contain several lines... So anyway, I am lost.

No, no. You understand it all too well. I haven't studied Martin's macro, but the expression you give will match exactly only Notes which have the same number at the beginning of the note and then at the end of the line, and if they have multiple lines they must have that same number at the end of every line. Just consider your sample:

I did indeed take the example text exactly literally My macro code expected the note text to be bookended by reference numbers. In thinking about it longer, I can see why this was an unrealistic assumption. But it's good advice that whenever a person requests a macro they should provide a real sample document, even just a small one, so the macro author can see what is actually required.

Thanks to Philip for coming through, both with a working macro and for explaining the code in my own misinformed macro

Finally the last special thing is the '¢' option. That allows the macro to later use the found bits from the expression as variables. You'll see in the code that a few lines later, Martin uses a variable '$0'. That refers to the found bit of this expression, i.e., the putative footnote/endnote reference number.

This is one of my favorite macro features. It's great to be able to easily refer to matches using automatically created macro variables. It works very nicely with named captures: