Page 1 of 1

displaying the number of found text

Posted: 2009-01-17 02:34:48
by js
Can the number of found text be caught in a macro? F.e. if yoy are only interested in how many times a selected textstring appears in the document, how would a macro look like that display number of found text, not leaving the actual caret postion?

Re: displaying the number of found text

Posted: 2009-01-19 14:57:42
by martin
That is something you can do:

Code: Select all

# save selection
$doc = Document.active
$selections = $doc.textSelections

# count found instances
$count = Find 'red', 'a'

# restore selection and prompt
$doc.setSelections($selections)
Prompt "Found $count instances."
I actually would have thought you could you use the "S" Find option, which means preserve the selection, but that only works for Replace commands. I'll have to file that as a bug. In the meantime, you could use the super secret "f" option, which would shorten the macro considerably:

Code: Select all

$count = Find 'red', 'af'
Prompt "Found $count instances."
This would also run quicker, if efficiency is important. Unfortunately the "f" option may change its letter at some point.

Re: displaying the number of found text

Posted: 2009-01-20 08:29:08
by js
Ever so happy to discover a little secret. If "f" changes we will change with it. Thanks for helping.

Re: displaying the number of found text

Posted: 2009-01-20 13:40:16
by martin
Glad to help! The next update will have the "S" option fixed for Find commands, so you can simply use "S" instead of "f" and it will work as intended.