Reply to topic  [ 4 posts ] 
Interleaving paragraphs in a document 
Author Message

Joined: 2006-06-04 15:42:19
Posts: 17
Location: Toowoomba, Australia
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


2010-11-28 02:43:07
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
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.


2010-11-29 15:53:45
Profile WWW

Joined: 2006-06-04 15:42:19
Posts: 17
Location: Toowoomba, Australia
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)

_________________
Jesus heals the rift between Man and Creator.


2010-11-29 18:07:49
Profile
Official Nisus Person
User avatar

Joined: 2002-07-11 17:14:10
Posts: 4251
Location: San Diego, CA
Excellent Paul, I'm happy to hear it's working well for you. Thanks for sticking with us from the Classic days! Best,
~Martin


2010-11-29 18:12:22
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: Bing [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software