Extremely basic question: How do I know if I found something

Get help using and writing Nisus Writer Pro macros.
Post Reply
waltzmn
Posts: 46
Joined: 2013-05-05 12:52:00

Extremely basic question: How do I know if I found something

Post by waltzmn »

Good people --

I have a situation where I need to have a macro check whether a file is in the proper format for a particular task. The way to test this is to see if it contains a series of text strings; if it has all of them, it is (almost certainly) right; if it doesn't, it's definitely wrong.

So what I need to do is perform a series of find commands, and know if they find something:

Code: Select all

For $Index = 1 to $NumberOfFields
   # Check to see that all fields are found.
   Find $FieldNames[$Index]
End     # End For $Index = 1 to $NumberOfFields
So I need something to tell me, after each "Find $FieldNames[$Index]," whether it found something or not. I don't need to know how many items it found; I don't need to save the selection; I don't need to display it; I just need to know, did the most recent find command find something or not?

There must be a way to do this, and once I see it, I'm sure I'll be very, very embarrassed, but I can't find it in the manual, and I'm not good enough at OOP to be able to guess. :-)

Thanks in advance.

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

Re: Extremely basic question: How do I know if I found something

Post by phspaelti »

Find returns the number of hits. So you can write

Code: Select all

If Find $Fieldname[$i]
...
End
Or

Code: Select all

$result =  Find $Fieldname[$i]
philip

waltzmn
Posts: 46
Joined: 2013-05-05 12:52:00

Re: Extremely basic question: How do I know if I found something

Post by waltzmn »

phspaelti wrote:
Find returns the number of hits. So you can write
(etc.)

Wonderful. Thank you!

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

Re: Extremely basic question: How do I know if I found something

Post by phspaelti »

My previous reply was a bit short due to circumstances. I just wanted to point out 1 or 2 things.

The Find command returns the number of hits—0 for none, otherwise a positive number—which is why it's possible to check it with if. However the Find command will work via the GUI, which is slow and will change your current selection. If all you want to do is check the document, it would be much better to use the .find command on the document text object. In this case things change a bit. The .find generally returns a Text Selection, however if you use .findAll you will get an array of Text Selections. When the find is unsuccessful then .find will return @undefined, while .findAll still returns an Array, just an empty one.
But if all you're aiming for is a simple check .find is probably the best option, and the following would work.

Code: Select all

$doc = Document.active
$check = @true
foreach $field in $fields
  if ! $doc.text.Find($field)
    $check = @false
    break
  end
end
prompt "The file's status is $check"
PS: I understand that real men raised on C and Java might think loops over arrays need to be written to iterate over an index, but I have allowed myself to use foreach instead :wink:
philip

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

Re: Extremely basic question: How do I know if I found something

Post by phspaelti »

phspaelti wrote:
2019-12-17 16:24:56
…however if you use .findAll you will get an array of Text Selections. When the find is unsuccessful then … .findAll still returns an Array, just an empty one.
And so if you wanted to do the same exercise in a situation where you need to use .findAll, then you will need to test the .count of the result:

Code: Select all

if $doc.text.findAll($field).count
etc.
This is because "if Array" will always return @true if the array exists, even if the array is empty.
philip

waltzmn
Posts: 46
Joined: 2013-05-05 12:52:00

Re: Extremely basic question: How do I know if I found something

Post by waltzmn »

phspaelti wrote:
The Find command returns the number of hits—0 for none, otherwise a positive number—which is why it's possible to check it with if. However the Find command will work via the GUI, which is slow and will change your current selection. If all you want to do is check the document, it would be much better to use the .find command on the document text object. In this case things change a bit. The .find generally returns a Text Selection, however if you use .findAll you will get an array of Text Selections. When the find is unsuccessful then .find will return @undefined, while .findAll still returns an Array, just an empty one.
As you correctly guessed in another post, I was raised on older programming languages (BASIC, C, FORTRAN, Pascal -- even some COBOL, gag), so yes, I do tend to think in that way.

Speed probably isn't an issue in this case, but my macros have never really used most of the features of the macro language, so examples really help. Thank you!

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

Re: Extremely basic question: How do I know if I found something

Post by phspaelti »

Obviously speed isn't going to be a real factor if the macro is simply checking one file. As a test I had it check 4 words in the Nisus Macro Language reference 1000 times. The Find version took about 17 seconds , while the .find version took about 2. But most of all the Find version was busy jumping around redrawing the screen and idly selecting words in the document. The .find version works more quietly.
The .find version would be something that you could use to check documents in the background, for example running it on a folder and (visibly) opening only those files that actually check out.
philip

Post Reply