Hello Nobumi,

I’m sending another version of the macro. It’s very similar, but instead of doing all that checking for conditions, it works differently.
As a first step it processes all the notes and puts them in a Hash where the footnote number is the key. Then it uses the reference number to find the matching note text.

This has several advantages:
- It will work, even if the number of references and footnotes are different. It will just match those references that have a matching footnote, and ignore the others.
- It will work even if the footnotes (or the reference numbers in the text) are not in order
- It will work for subsets of references/footnotes, or if the references don’t start numbering at 1
- It actually makes for a shorter macro

Also it would be able to modify it to report any missed references or leftover footnotes.

The limit cases where it will fail are where you have two identical reference numbers or two footnotes with identical numbers.

# Assumes the front two documents are the ones we want
$main_doc = Document.openDocuments[0]
$notes_doc = Document.openDocuments[1]

# Get all the notes as well as the references in the main document
$note_Refs = $main_doc.text.find '\(\d+\)', 'Ea'
$notes = $notes_doc.text.find '^\d+\. .+', 'Ea'

# Put all the notes in a hash with the footnote number as the key
$note_texts = Hash.new
foreach $note in $notes
    # Get the matching note text and its number
    $note.subtext.find '(?<fn_num>\d+)\. (?<fn_text>.+)', '$E'
    # Store it in the hash
    $note_texts{$fn_num} = $fn_text
end

# Process each reference in the main doc from back to front
foreach $ref in reversed $note_Refs

    # Get the number 
    $ref.substring.find '(?<num>\d+)', '$E'

   # Check that the footnote number matches
    if $note_texts.definesKey($num)
        # Remove the parentheses and reference number from the text
        $ref.text.deleteInRange $ref.range
        # Insert a footnote instead
        Note.insertFootnoteInTextAtIndex $ref.text, $ref.location, $note_texts{$num}
   end
end


On 2016 Jul 14, at 08:15, Nobumi Iyanaga <[log in to unmask]> wrote:

Hello,

Here is the macro I would like to have -- that I tried to write myself, but this seems too difficult for me.

I have two documents, one containing the main text with footnote numbers in plain text (in the format "(x)"), and the other containing all the notes, each consisting in one line, beginning with "1. ", "2. ", etc.

In other words, the two files are like this:

main text document:

aaaaaaaaaa (1), bbbbbbbb (2) ccccccc (3)...

notes document:

1. xxxxxxxx
2. yyyyyyy
3. zzzzzzzz

etc.

The regex "\(\d+\)" finds all the note numbers in the main text, and nothing else; and the regex "^.+$" finds all the note contents.

I would like to make a new document in which the notes will be incorporated as footnotes. How can I do this?

Thank you very much in advance for your insight.

Best regard,

Nobumi Iyanaga
Tokyo,
Japan

Philip Spaelti