Page 1 of 2
Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 2:36 pm
by totallyLost
Why can I not use the following script to set the color of letters to black and numbers to red in a particular field?
===
repeat with i= 1 to the number of characters in fld 1
if the isNumber of character i of fld 1 is true then
set textColor of character i of fld 1 to red
else
set textColor of character i of fld 1 to black
end if
end repeat
===
No-clue Newbie
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 3:02 pm
by jmburnod
Hi,
I tested your script and it works here.
Sorry no idea why.
best regards
Jean-Marc
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 3:11 pm
by dunbarx
Hi.
There is no issue with setting the textColor of a character to whatever you want. But a character does not have the ability to hold a custom property. LC does not provide for it.
So you must rethink how you want to do this. Something like:
Code: Select all
put "abcdefghijlklmnopqrstuvwxyz" into letterList
repeat with y = 1 to the number of chars of yourText
if char y of yourText is an integer then set the textColor of char y of yourText to red
if char y of yourText is in letterList then set the textColor of char y of yourText to black
end repeat
Craig
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 3:13 pm
by dunbarx
Bernd.
I was unable to either set or access any sort of custom property of a single character residing in a field. I assumed even before I tried it that a character is not any sort of LC control, and could not "hold" a custom property.
Same with "word" or "line" or any sort of direct reference to actual text.
Am I wrong?
Craig
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 3:35 pm
by Klaus
Hi Craig,
ISNUMBER is a valid FUNCTION in LC, although it sounds like the name of a custom property,
we have here a case of rare but nevertheless valid use of the syntax:
Code: Select all
...
answer isnumber(88)
-> TRUE
answer the isnumber of 88
-> TRUE
...
Best
Klaus
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 3:56 pm
by dunbarx
Klaus.
I know about "isNumber" being a function in LC. But it refers to a value, and is not a property of a character, word or line in a field or variable.
The problem with the OP's original post is that he is trying to ascribe that function to an element of field text as if it was a property of that element of text. That dog don't hunt.
Craig
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 4:33 pm
by Klaus
Craig, you don't get it!
ISNUMBER is a function which returns TRUE or FALSE if the parameter in question is a number or not!
Code: Select all
...
put isnumber("a string")
## -> FALSE
put isnumber("666")
## -> TRUE
put isnumber(666)
## -> TRUE
...
put "This is a string and a number 6" into tString
put isnumber(char -1 of tString)
## -> TRUE
...
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 6:13 pm
by bn
dunbarx wrote: ↑Mon Apr 07, 2025 3:13 pm
Bernd.
I was unable to either set or access any sort of custom property of a single character residing in a field. I assumed even before I tried it that a character is not any sort of LC control, and could not "hold" a custom property.
Same with "word" or "line" or any sort of direct reference to actual text.
Am I wrong?
Craig
Craig,
You can set the metadata but not custom properties of text elements of fields. Also of lines.
Kind regards
Bernd
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 6:18 pm
by dunbarx
Klaus.
I do get it. I am not talking about what "isNumber", in and of itself, can or cannot do. I am trying to solve the OP's issue. And that is beyond what the "isNumber" function can do.
Say he has the following in a field 1:
AC,D&4(w)6
and this in a button script:
Code: Select all
on mouseUp
get fld 1
repeat with y = 1 to the number of chars of it
put isNumber(char y of it) & comma after temp
end repeat
end mouseUp
The result will be:
false,false,false,false,false,true,false,false,false,true,
So my offering early in this thread deals with the actual situation, digits or letters of the alphabet, and that cannot involve "isNumber". A char like "&" will be colored like a "W". Only the OP knows if this is what he wants for such chars.
Craig
EDIT- Klaus or Bernd, if you saw this right after I posted, I corrected some errors I had.
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 6:19 pm
by dunbarx
Bernd.
Good point. But my 5-line handler does a decent job all on its own.
Craig
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 6:25 pm
by jacque
Craig, he's using the alternate function syntax, like you can use date() or "the date". The original handler works for me too. But I did get an error when referencing an empty field by accident.
If the field has a lot of text, using htmltext will be much faster. Iterating on shorter text should be fast enough though.
Re: Sorting out letters from numbers in a field
Posted: Mon Apr 07, 2025 6:31 pm
by dunbarx
Jacque.
I get that. OK.
It is still up to the OP to answer whether he wants to color a char such as "&" in the same way as any other non-digit char. If he does, in other words there are exactly two sets of chars, integers and absolutely everything else, then "isNumber" works fine. I read his first post as not that case, "letters and numbers", but I may have read too little into it.
Craig
Re: Sorting out letters from numbers in a field
Posted: Tue Apr 08, 2025 1:40 pm
by totallyLost
Thank you so much for all your input!
Jean-Marc, thanks to you, I found out an extremely stupid mistake of mine and I realized the script I wrote actually works. (For you guys' amusement to know my mistake, I forogt to insert "on mouseup" and "end mouseup"!)
Craig, thanks for letting me know another way; I tried your script and of course it works! (By the way, I don't need to color a char such as "&".)
Thanks to Klaus and Bernd and Jacque for getting me curious to try to understand further! I asked Grok 3 (before I realized my script was ok) and it gave me a script which also works:
on setColors -- Define a handler to call the script
repeat with i = 1 to the number of chars in field 1
if isNumber(char i of field 1) then
set the textColor of char i of field 1 to "255,0,0" -- Red
else
set the textColor of char i of field 1 to "0,0,0" -- Black
end if
end repeat
end setColors
=====
Groks asked me to correct isNumber Syntax, saying,
"Changed the isNumber of character i of fld 1 to isNumber(char i of field 1). The isNumber() function takes a value as an argument and returns true or false."
Which, as a newbie always, is a little difficult for me to get.
But the important things is, thanks to you all, I get it done and I was able to learn more (and I know I was right: asking people is always better than asking AI); thank you, thank you, thank you all!!!
Re: Sorting out letters from numbers in a field
Posted: Tue Apr 08, 2025 1:52 pm
by richmond62
While it is undoubtedly useful to determine where something is a number or an alphabetic form, what also might be useful is to determine whether something is written is some language or not:
'Hello, Happy People' might return 'English', or, at least 'Latin' (as in alphabet), while
'Здрасти, хора' might reutrn 'Bulgarian', or, at least 'Cyrillic'.
But I am not sure whether that could be done without leveraging Unicode ranges.
Re: Sorting out letters from numbers in a field
Posted: Tue Apr 08, 2025 3:01 pm
by dunbarx
Well then, welcome to LC and this forum.
...By the way, I don't need to color a char such as "&".)
That was my point, that using "isNumber" only tells you if the argument to the function is, er, a number. In other words, there are other chars in a body of text besides numbers and the "ABC's. For what I think are your purposes, you are probably safe in thinking there are three character sets, the third set being any character one can type that is not a member of the other two. In that case you need something like my original handler.
Craig