Select and put in array.

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Select and put in array.

Post by loulesko »

Hello

I am trying to find all occurrences of text in a paragraph style and put them in an array. I have the regex part just fine, creating an array with the found or selected text is eluding me. For example if I wanted to put all occurrences of Heading 1 in an array.

Thanks
Lou
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Select and put in array.

Post by phspaelti »

Hello Lou,
Non-contiguous selections in NWP are already in an array. For example if you have used a find command to make such a selection then

Code: Select all

$array = $doc.textSelections
will return an array. However the array will be one containing selection objects. This is usually good enough, and can even be an advantage, if you want to process the array serially.

However if for some reason you would like to have just the selected subtext (or substrings) in an array, you can achieve that with the handy command .arrayByMakingValuesDoCommand. This command requires an array of objects and an object command or property. It then creates a second array with the same number of elements by applying the given command to each object in the original array.
So you can get just the selected subtexts of a non-contiguous selection like this:

Code: Select all

$array = $doc.textSelections.arrayByMakingValuesDoCommand 'subtext'

This one line is the programming equivalent of writing the following code:

Code: Select all

$array = Array.new
foreach $sel in $doc.textSelections
    $array.push $sel.subtext
end
Finally, note that the Nisus macro language allows you to create such text selections without going through the visual interface. So you could put the text of all your Heading 1 paras into an array like this:

Code: Select all

$doc = Document.active
$h1Style = $doc.styleWithName 'Heading 1'
$array = $doc.text.findAll($h1Style).arrayByMakingValuesDoCommand 'subtext'
Last edited by phspaelti on 2023-01-10 18:50:29, edited 3 times in total.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Select and put in array.

Post by phspaelti »

I just want to add one more thing here, which is tangential to the above discussion. If you use the Find All style method to make a selection, NWP will find the relevant paragraphs, but the returned subtexts will all contain the final newline for each paragraph. If you want to get rid of these, then things get a bit ugly. In the end the easiest and cleanest way is probably just to process them serially, which is why working with the original selection objects can be easier than putting the subtexts into an array. For example you could get the subtexts without the newlines as follows:

Code: Select all

$doc = Document.active
$sels = $doc.text.findAll $doc.styleWithName('Heading 1')
$array = Array.new
foreach $sel in $sels
    $range = $sel.text.rangeOfParagraphContentAtIndex $sel.location
    $array.push $sel.text.subtextInRange $range
end
This is probably simpler than any approach which first puts the subtexts into an array.
philip
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Select and put in array.

Post by loulesko »

Philip,

This is amazing, thank you so much. You have saved me a ton of time and you've educated me on how some of the more advanced macro features work. Very much appreciated.

Lou
Post Reply