Create lines between list items

Get help using and writing Nisus Writer Pro macros.
Post Reply
davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Create lines between list items

Post by davidrcalvert » 2016-08-17 16:11:57

Need some help on creating a Macro that would create lines between items on a list.
For example if I have a bullet list :

•word
*word2
*word3

and I want to create empty lines after list items such as

•word


*word2


*word3


How would I do this with a Macro?



Thanks,
David

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

Re: Create lines between list items

Post by phspaelti » 2016-08-17 17:57:28

Hello David,

Let me first say, that if it were up to me, I wouldn't add empty lines into a list. It goes against the logic of a document to do that, and using empty paragraphs for formatting is poor formatting style. With a bullet list you can sort of get away with this, but if the list is a numbered list it will reset your numbering to "1" for each list item. How about just increasing the "space after" on the list items? I would use a paragraph style—with a name like "Bullet list with space"—and attach the list style and the extra large "space after" to that style.

Apart from that, writing a macro to do what you want would consist of the following steps:
  1. Select the bullet list
  2. Add empty lines
  3. Turn off the list style on the empty lines (to get rid of the bullets)
The first step can be done using a find by style:

Code: Select all

$doc = Document.active
$bulletList = $doc.styleWithName "Bullet List"
Find All $bulletList
Now for the second step you would like to do this:

Code: Select all

Find And Replace '^', '\n', 'Eas'
But this will typically add empty lines before and after the list as well. So you precede this line with a line that selects only the returns that are completely within the list:

Code: Select all

Find All in Selection '\n.', 'Eas'
Find And Replace '^', '\n', 'Eas'
Now at that point, I would have expected Nisus to end with the empty lines selected, but unfortunately this is not the case. Basically Nisus does not preserve selections on a find/replace in a non-contiguous selection. So at that point you have two options: work around this, or skip using the GUI Find command. I tend to favor the latter option. But this does get a bit ugly, because you either need to turn off the list style one line at a time, or calculate the location of the inserted bits (counting the shifted selections) so you can select them at the end.

Like I said, I would just use "space after". :wink:
philip

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

Re: Create lines between list items

Post by phspaelti » 2016-08-17 19:23:47

One keeps learning new things…

Apparently, if you just insert a newline in a list—as opposed to pressing return, or using find/replace to insert it—you get an empty line without a list item. At which point the macro becomes much shorter. Here is my version:

Code: Select all

$doc = Document.active
$bulletList = $doc.styleWithName "Bullet List"
$bulletListSels = $doc.text.findAll $bulletList
$emptyLine = Cast to String "\n"

foreach $listSel in reversed $bulletListSels
   $i = $i = $listSel.text.rangeOfParagraphAtIndex($listSel.bound - 1).location
   while $i > $listSel.location
      $listSel.text.insertAtIndex $i, $emptyLine
      $i = $listSel.text.rangeOfParagraphAtIndex($i - 1).location
   end
end
And here as a macro:
Attachments
Add Empty Lines to List.nwm
(3.81 KiB) Downloaded 90 times
philip

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

Re: Create lines between list items

Post by phspaelti » 2016-08-17 19:42:46

And as they say, there's more than one way to do it.
One can use the macro language ListItem object, and then it comes out like this:

Code: Select all

$doc = Document.active
$bulletList = $doc.styleWithName "Bullet List"
$bulletListSels = $doc.text.findAll $bulletList
$emptyLine = Cast to String "\n"

foreach $listSel in reversed $bulletListSels
    foreach $item in reversed ListItem.itemsIn($listSel)
        $loc = $item.enclosingTextRange.location
        if $loc > $listSel.location
            $item.enclosingText.insertAtIndex $loc, $emptyLine
        end
    end
end
philip

davidrcalvert
Posts: 28
Joined: 2014-07-18 18:32:13

Re: Create lines between list items

Post by davidrcalvert » 2016-08-20 14:43:24

Hi Phillip,

Thank You. The main reason behind this Macro Request is that I get a lot of requirements in the form of a bullet list and my responses to each item in the list follows each one. Since I usually respond with 2-4 lines to each list item , I was trying to create the space necessary between each item.

Your Macro does just that. Thank You

The only other variation of the Macro you provided that I would need help as well would be the following.

Have the ability in the Macro to custom create 2, 3. 5, 5 lines of space.


Also use the Macro on paragraphs to create space between paragraphs as well .

Again, Thank You for your help. I appreciate it.

David

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

Re: Create lines between list items

Post by martin » 2016-09-21 15:58:49

Just one tip:
phspaelti wrote:I would have expected Nisus to end with the empty lines selected, but unfortunately this is not the case. Basically Nisus does not preserve selections on a find/replace in a non-contiguous selection.
You should be able to have Nisus Writer preserve your multi-part selection after a Replace All by using the "S" option. For example:

Code: Select all

Find and Replace 'a', 'xyz', 'EasS'
That will replace all letter a's in the selection with xyz, while keeping the existing selection intact (multipart or not).

Post Reply