Using the *TypeOf" command produces "text" even for a numerical text selection.
How can one test if a selection is a number?
How to test if a selection is a number or text?
Re: How to test if a selection is a number or text?
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:
(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.
Code: Select all
$sel.subtext.find '\d+', 'E'

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
Re: How to test if a selection is a number or text?
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.
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.
Re: How to test if a selection is a number or text?
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:This produces only a reminder that "The Text object does not have a 'subtext' property".
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
philip
Re: How to test if a selection is a number or text?
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.
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
Re: How to test if a selection is a number or text?
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.