Normal to Body Text

Get help using and writing Nisus Writer Pro macros.
Post Reply
ToddO
Posts: 33
Joined: 2015-10-18 14:25:32

Normal to Body Text

Post by ToddO » 2019-07-21 15:38:39

I have a few documents where the main body text is in Normal style. I need to convert them to a Body Text Indent style, which is easy enough to make a macro for.

Where I'm stuck is that the first paragraph of each section of body text should not be indented. (It should be in a style called Body Text.) In other words, following any style that is not Body Text Indent, I need to apply Body Text to look Like this:

Heading 1
Body Text
  Body Text Indent
  Body Text Indent
Heading 2
Body Text
  Body Text Indent
  Body Text Indent

I can't figure out how to look through the paragraphs to apply Body Text following any paragraph that is not Body Text Indent. Anyone have any ideas on how to make that work?

User avatar
phspaelti
Posts: 1022
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Normal to Body Text

Post by phspaelti » 2019-07-21 20:06:59

Hello Todd,
Assuming you want hints about how to solve this rather than a fully written out solution, let me suggest this.
Start by giving all Body Text the same style. If you select these, then you will have an array of selections. The index/location of each selection block is the index of the first paragraph in that block, i.e., the ones you want to apply the "no-indent" style. You can go through them in turn, or use the macro language to create a non-contiguous selection for these paragraphs.

Hope this helps.

PS: I'll post a macro solution later, if this is still too obscure.
philip

martin
Official Nisus Person
Posts: 4584
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Normal to Body Text

Post by martin » 2019-07-22 07:51:40

Philip has some good advice here, as usual. One tip regarding this point:
phspaelti wrote:
2019-07-21 20:06:59
You can go through them in turn, or use the macro language to create a non-contiguous selection for these paragraphs.
Although a macro could apply and fix styles paragraph by paragraph, working its way through the document, it's going to be more efficient to reformat everything at once. In other words, your macro will be much faster if you can create a single multi-part selection that includes all target paragraphs, and then apply the desired style in a single action.

That doesn't mean the macro can't still identify target paragraphs one at a time. You can build the list of targets using a strategy that maybe looks something like this:

Code: Select all

$targets = Array.new
Select Document Start
While Select Next Paragraph
	$selection = TextSelection.active
	$styleName = $selection.paragraphStyle.name
	# decide if paragraph should be processed
	$targets.appendValue($selection)
End

$doc = Document.active
$doc.setSelection($targets)
# format all selections at once
However, if your macro can avoid a loop like that completely, that's going to be the very fastest. For example, selecting all paragraphs using the "Normal" style (so as to convert them to "Body Text" style) can be achieved using a single find command.

If you still need more help or a working macro, please let us know.

User avatar
phspaelti
Posts: 1022
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Normal to Body Text

Post by phspaelti » 2019-07-24 06:22:41

Here is how I would use the macro language to solve this problem:

Code: Select all

# Select First Paragraph of Each Selection Block
$doc = Document.active
$sels = $doc.text.find $doc.styleWithName('Normal'), 'a'
$firstParas = Array.new
foreach $sel in $sels
    $firstParas.push TextSelection.new $sel.text, $sel.text.rangeOfParagraphAtIndex($sel.location)
end
$doc.setSelection $firstParas
This macro will use the style name to find all blocks with Normal style. Then it selects just the first paragraph of each block. Now apply Body Text. Then apply Body Text Indent to the remaining Normal style paragraphs.
philip

ToddO
Posts: 33
Joined: 2015-10-18 14:25:32

Re: Normal to Body Text

Post by ToddO » 2019-07-24 23:59:56

Thanks. I’ll give it a shot.

ToddO
Posts: 33
Joined: 2015-10-18 14:25:32

Re: Normal to Body Text

Post by ToddO » 2019-07-26 15:04:49

Thanks to you both. It works, but there are some areas I'm still researching in the Macro reference to figure out how.

The Final result looked much like Philip's code:

Code: Select all

# Select First Paragraph of Each Selection Block
$doc = Document.active
$sels = $doc.text.find $doc.styleWithName('Normal'), 'a'
$firstParas = Array.new
foreach $sel in $sels
    $firstParas.push TextSelection.new $sel.text, $sel.text.rangeOfParagraphAtIndex($sel.location)
end
$doc.setSelection $firstParas
Body Text

# Find Remaining Normal Paragraphs
$sels = $doc.text.find $doc.styleWithName('Normal'), 'a'
$doc.setSelection $sels
Body Text Indent

martin
Official Nisus Person
Posts: 4584
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Normal to Body Text

Post by martin » 2019-07-29 08:29:23

ToddO wrote:
2019-07-26 15:04:49
Thanks to you both. It works, but there are some areas I'm still researching in the Macro reference to figure out how.
It's good to hear you've made progress! Please let us know if there's anything else we can do to help you.

Post Reply