nisus.com
https://www.nisus.com/forum/

Interleaving paragraphs in a document
https://www.nisus.com/forum/viewtopic.php?f=17&t=3995
Page 1 of 1

Author:  paulcruice [ 2010-11-28 02:43:07 ]
Post subject:  Interleaving paragraphs in a document

Greeting all.
I have a NW Pro document with an EVEN number of paragraphs as exemplified below.

EnglishPara1
EnglishPara2
EnglishPara3
EnglishPara4
GreekPara1
GreekPara2
GreekPara3
GreekPara4

I need to create a Macro which moves GreekPara4 to after EnglishPara4. Then move GreekPara3 to after EnglishPara3 and so on till I have GreekPara1 after EnglishPara1.

I have tried doing this with Applescript but NW Pro just doesn’t seem to support Applescript well.
I can do it in Pages using a Applescript and then copy it to NW Pro but I would much prefer to do it all in NW Pro if possible.
Actually, I don’t care how this interleaving is done. I used the method above in the Pages Applescript for economy of script reasons.

Would appreciate any help as I don't know where to start in writing a NW Pro Macro.
Thanks.
PC

Author:  martin [ 2010-11-29 15:53:45 ]
Post subject:  Re: Interleaving paragraphs in a document

Hi Paul.. this ended up taking more code than I thought, but here you are:
Code:
$paraCount = 0
$interleave = Array.new

# Make sure the last paragraph in the document ends with a newline
$doc = Document.active
$text = $doc.text
If $text.length > 0
   $lastChar = $text.characterAtIndex($text.length - 1)
   If 10 != $lastChar
      $text.insertAtIndex($text.length, "\n")
   End
End

# count up the number of paragraphs in the document
Select Document Start
While Select Next Paragraph
   $paraCount += 1
End

$parity = $paraCount % 2
If 0 != $parity
   Prompt "An even number of paragraphs is required.", "Your document has $paraCount paragraphs."
End

# Extract all the paragraphs to interleave. We load the array backwards for speed and easy coding
$count = $paraCount / 2
While $count > 0
   $selection = TextSelection.active
   # store paragraph in array for interleaving
   $paraText = $selection.subtext
   $interleave.appendValue($paraText)
   # delete the paragraph
   $selection.text.deleteInRange($selection.range)
   # move next
   Select Previous Paragraph
   $count -= 1
End

# now interleave the paragraphs, also going backwards
$count = $paraCount / 2
While $count > 0
   $interleaveIndex = $interleave.count - $count
   $selection = TextSelection.active
   $paraText = $interleave.valueAtIndex($interleaveIndex)
   $selection.text.insertAtIndex($selection.bound, $paraText)
   # move prior
   Select Previous Paragraph
   $count -= 1
End

The general idea is to slurp up all the paragraphs from the latter half of your document into an array ($interleave) and then go back and reinsert them into the remaining paragraphs. Let me know if you have any questions.

Author:  paulcruice [ 2010-11-29 18:07:49 ]
Post subject:  Re: Interleaving paragraphs in a document

Hi Martin,
Thanks so much for the macro. It works brilliantly.
I can't even pretend to understand the bits to the macro.
My Applescript in Pages was much much shorter but works at 1000 times slower than your macro.
Your macro works almost instantaneously.
I appreciate the time you have spent on this.

For those students of the bible, this macro makes it very easy to put the Greek (or Hebrew) text underneath an English translation.

Martin, thanks again.
And as always all the best for Nisus Writer Pro.
I love it.

regards,
Paul Cruice
Australia
(an ancient Nisus user)

Author:  martin [ 2010-11-29 18:12:22 ]
Post subject:  Re: Interleaving paragraphs in a document

Excellent Paul, I'm happy to hear it's working well for you. Thanks for sticking with us from the Classic days! Best,
~Martin

Page 1 of 1 All times are UTC - 8 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/