Page 1 of 1

Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-06-10 03:36:56
by Nobumi Iyanaga
Hello,

I need a macro that inserts the paragraph numbers at the beginning of all paragraphs in a document, with four digits (like "0001", "0002"), and a "tab".

I looked for if such a macro is not already in this forum, and found at least a candidate in the topic "Auto increment applescript to number paragraphs in BBEdit" (begun on 2010-03-12). Philip Spaelti proposed the following macro:

Code: Select all

# Number Paragraphs with Carets

$doc = Document.active
$text = $doc.text

$carets = $text.findAll '^\^', 'Ea'

$num = $carets.count
foreach $caret in reversed $carets
  $numString = Cast To String $num
  $numString.replaceAll '\d{3}$', ',\0', 'E'
  $text.replaceInRange $caret.range, $numString
  $num -=1
end
I tried it, but it didn't anything, it seems.

It seems that the "find" expression '^\^' doesn't find anything...??

I changed it to a simple "^"; this inserted some numbers in the documents, but in wrong places...

I would very much appreciate if anyone could show me a working macro.

Thank you very much in advance!

Best regard,

Nobumi Iyanaga

Re: Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-06-10 09:10:12
by phspaelti
Hi Nobumi,
That macro was intended to number paragraphs that had actual caret symbols (^) as the beginning character. If you remove the '\^'. It will find all paragraph beginnings, but there is a hitch, for which the macro is not written safely. The $text.findAll command is going to find all paragraph beginnings included in the text object. This will mean also things like headers, footers, table cells, and maybe others (text boxes? comments? I'm not sure.) So that's probably why your modification did not give you the expected result.

To get around this problem there are a number of different ways. The most common way that I would use is to only number paragraphs with some actual text. With that assumption, and your wish for the numbers padded to four digits, the following might work:

Code: Select all

# Number Lines padded to 4 digits
$doc = Document.active
$lineStartSels = $doc.text.findAll '^.', 'Ea'
foreach $i, $sel in reversed $lineStartSels
    $num = '000' & ($i + 1)
    $num = $num.substringInRange(Range.new($num.length - 4,4))
    $sel.text.insertAtIndex($sel.location,($num) & "\t")
end
Let me know, if that still doesn't help.

Re: Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-06-10 15:46:31
by Nobumi Iyanaga
Hello Philip,

Thank you as always for your excellent macro. It works very well.

However, there is a problem: that is the macro inserts the number "0001" at the beginning of the footer (having the page number...), so that all the digits are the actual number of the paragraphs + 1. I thought that limiting the search area to the main document body would solve the problem, so that I changed the search line to:

Code: Select all

$lineStartSels = $doc.text.findAll '^.', 'Ea', 'm'
but the result was the same...

On the other hand, I thought it would be handy if with another macro, I could limit the paragraphs to which I would insert the numbers to the current selection; I thought changing the search line to:

Code: Select all

$lineStartSels = $doc.text.findAll '^.', 'Eas'
would do the trick, but this did nothing at all.

Thank you very much in advance for your insights!

Re: Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-06-10 16:24:50
by phspaelti
Hello Nobumi,
The problem must be that you have some actual text in the footnote (e.g. the page number). Your first attempt to fix it is the right one. You should use the "where" attribute to limit the search. In your case you used "m" to indicate "main body", but you also need to specify "-h" for "not header/footer". You may want to add other minuses, depending on your use, e.g. "-n" for "not in notes", or "-c" for "not in comments". So the corrected line will look like this:

Code: Select all

$lineStartSels = $doc.text.findAll '^.', 'Ea', 'm-h-n-c'
On the other hand to limit the macro to a selected area requires a completely different approach. This is because the "Text.find" command is working on objects internally, and it doesn't know anything about selections. So the usual "s" option doesn't work. [There is no "Text.findInSelection" command.]

For that case you have to do something much more complicated like this:

Code: Select all

# Number Lines padded to 4 digits in current selection only
$sel = TextSelection.active
$text = $sel.text
$selStart = $sel.location
$lineStartSels = $sel.substring.findAll '^.', 'Ea'
$lineStarts = $lineStartSels.arrayByMakingValuesDoCommand 'location'
foreach $i, $loc in reversed $lineStarts
    $num = '000' & ($i + 1)
    $num = $num.substringInRange(Range.new($num.length - 4,4))
    $text.insertAtIndex($selStart + $loc,($num) & "\t")
end

Re: Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-06-10 18:37:19
by Nobumi Iyanaga
Hello Philip,

The two macros work perfectly now. Thanks so much!

Re: Insert paragraph numbers at the beginning of paragraphs

Posted: 2018-07-28 00:48:46
by Bob Stern
Thanks, Philip, for your excellent method of padding the number to 4 digits!

I use paragraph numbering rather than line numbering, but I have the same requirement to pad with zeros. For years I've been using the awkward solution of 3 different paragraph styles for paragraph numbers in the range of 1-9, 10-99, and 100+. As you can imagine, this was error-prone when edits caused a paragraph to move from the >9 range to the <10 range, or vice versa.