Page 1 of 1

Interleaving paragraphs in a document

Posted: 2010-11-28 02:43:07
by paulcruice
Greeting all.
I have a NW Pro document with an EVEN number of paragraphs as exemplified below.


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.

Re: Interleaving paragraphs in a document

Posted: 2010-11-29 15:53:45
by martin
Hi Paul.. this ended up taking more code than I thought, but here you are:

Code: Select all

$paraCount = 0
$interleave =

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

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

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

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

# now interleave the paragraphs, also going backwards
$count = $paraCount / 2
While $count > 0
	$interleaveIndex = $interleave.count - $count
	$selection =
	$paraText = $interleave.valueAtIndex($interleaveIndex)
	$selection.text.insertAtIndex($selection.bound, $paraText)
	# move prior
	Select Previous Paragraph
	$count -= 1
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.

Re: Interleaving paragraphs in a document

Posted: 2010-11-29 18:07:49
by paulcruice
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.

Paul Cruice
(an ancient Nisus user)

Re: Interleaving paragraphs in a document

Posted: 2010-11-29 18:12:22
by martin
Excellent Paul, I'm happy to hear it's working well for you. Thanks for sticking with us from the Classic days! Best,