Part 2:
Maybe my earlier post answered all your questions, but I just have the feeling that maybe you wanted to do something else. If you really just wanted to copy all the find bits, you don't need a loop, in fact you don't need a macro. So maybe what you were really looking for is a way to do something to each found bit individually. As already mentioned you could use the
while loop I just mentioned, but the truth is, this isn't really the best way to do things.
There is also another point: as you remember NWC, in NWC the clipboard was a really important part of writing macros. But in the current version of Nisus, we don't need to use the clipboard anymore.
So if you want to find all bits and do something to each bit, that is exactly what the
foreach loop is for. For 90% (or maybe even 99%) of all loop tasks, I recommend using a
foreach loop. This works like this.
When you do a
Find All Nisus creates a non-contiguous (text) selection, that is, an
array of individual
text selections. The foreach loop allows you deal with arrays one piece at a time. It will know how many there are, and when to stop, etc.
To get the non-contiguous selection, we do need one more thing, the
document object. You can get the document object with the following bit of code:
And with that we have what we need to make a loop. For your case that would look like this:
Code: Select all
$doc = Document.active
Find All @Text<(:?[^\t]*)>, 'E-i'
foreach $sel in $doc.textSelections
… do stuff here …
end
Of course the (do stuff here) is where the action is. At that point you can do things to that one selected bit using the $sel variable. Since the $sel is a text selection. it will not only be able to tell you what the string content of the selected bit is, but also where in the file it is.
Finally one last point. If the (do stuff here) includes changing the text (adding or deleting) it is best to always work backwards. The reason is that Nisus deals with selections by counting characters, and if you change the file the number of characters will be off. If you work backwards, the ones you change will always be at the end, and so the ones you still have to deal with will still be in the same place. Anyhow to work backwards we just have to add the word
reversed:
Code: Select all
$doc = Document.active
Find All @Text<(:?[^\t]*)>, 'E-i'
foreach $sel in reversed $doc.textSelections
… do stuff here …
end