How to test if a selection is a number or text?

Get help using and writing Nisus Writer Pro macros.
Post Reply
js
Posts: 229
Joined: 2007-04-12 14:59:36

How to test if a selection is a number or text?

Post by js » 2013-12-23 03:36:13

Using the *TypeOf" command produces "text" even for a numerical text selection.
How can one test if a selection is a number?

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

Re: How to test if a selection is a number or text?

Post by phspaelti » 2013-12-23 20:32:35

A (text) selection subtext is always going to be a text object. If you want to know whether the text is digits you could use find:

Code: Select all

$sel.subtext.find '\d+', 'E'
(or something more carefully worked out :) )
But maybe you don't really need to test? You could just use Cast to Int or Cast to Float. If the text is a number you will get a number otherwise you will get 0. It would all depend on what you are trying to achieve.
philip

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: How to test if a selection is a number or text?

Post by js » 2013-12-25 09:03:27

This produces only a reminder that "The Text object does not have a 'subtext' property".

And yes, I need the test. It serves to look up either a chapter references or text string within a file. The macro should do differently things depending on whether it's one or the other. The macro should be able to to know f.e. that if "12" is selected the selection is a number, and that if "Santaclaus" is selected the selection is a word. Nothing more and nothing less.

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

Re: How to test if a selection is a number or text?

Post by phspaelti » 2013-12-25 18:25:34

js wrote:This produces only a reminder that "The Text object does not have a 'subtext' property".
That just means that the variable you are using is already a text object (not a selection). So you can directly use .find.
js wrote:And yes, I need the test. It serves to look up either a chapter references or text string within a file. The macro should do differently things depending on whether it's one or the other. The macro should be able to to know f.e. that if "12" is selected the selection is a number, and that if "Santaclaus" is selected the selection is a word. Nothing more and nothing less.

Code: Select all

$sel = TextSelection.active
$selTxt = $sel.substring

if $selTxt.find('\d+', 'E')
	prompt 'I found a number'
else
	prompt 'No number found'
end
But as already said, depending on what you want you should formulate the find expression more carefully. This example will accept any selection that contains a digit somewhere.
philip

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

Re: How to test if a selection is a number or text?

Post by phspaelti » 2013-12-25 18:44:29

Anyhow in Nisus a selection (or more accurately, the subtext/substring of the selection) is always a text object, and so if you want to distinguish different selections, you should treat this as a text matching task.
The number/text distinction only comes into play, if you want to do calculations with it. But even in that case Nisus will just take care of the conversion (treating any input that does not look like a number as 0). Still it might be good form to use a Cast as command, before you do math with the selection.
philip

js
Posts: 229
Joined: 2007-04-12 14:59:36

Re: How to test if a selection is a number or text?

Post by js » 2013-12-26 11:24:33

Thank you, this works very well for me. The problem was to look up text on one hand and chapter references on the other hand with one single macro. As chapter references text appear in different formats, like "5.1" or "5:01" or "5.01" for one and the same thing, I wanted to treat these numeric searches separately, to allow searches in any of these formats. No need to recasting for this.

Post Reply