Page 1 of 1
Setting the text style of certain words in a field
Posted: Thu Jul 13, 2017 1:03 pm
by LouisThompson
Hi,
I've played around, searched, but not found anything...
(Sounds like the first line of a Country & Western song
Anyway, I have a field which contains a Python Script.
I'd like to set the text style of certain words...
eg
def, return, import, for : bold, blue
numbers should all be red
comments should be green
I'm happy to parse the field to find the relevant sections and words, but how do I apply the text style atrributes.
Thanks
Louis
Re: Setting the text style of certain words in a field
Posted: Thu Jul 13, 2017 1:16 pm
by bogs
I believe the htmlText property in the dictionary may be what you want. You can set many aspects of text styling with it, including colors. Alternately, textStyle / textColor could be used. Just type "text" in the dictionary, there is a lot there

Re: Setting the text style of certain words in a field
Posted: Thu Jul 13, 2017 1:50 pm
by dunbarx
What Bogs said.
The HTML text will allow you to attach style tags to text within, say, a variable. You need to research this as per his suggestion, since this is a powerful tool indeed.
But just to get back to basics, put some numbers and ordinary words in a field. Put this in a button script somewhere:
Code: Select all
on mouseUp
repeat with y = 1 to the number of words of fld 1
if word y of fld 1 is a number then set the textColor of word y of fld 1 to "yellow"
else set the textColor of word y of fld 1 to "blue"
end repeat
end mouseUp
This will require forethought to parse text , as you say, into several different categories. The "offset" function might come in handy...
Craig Newman
Re: Setting the text style of certain words in a field
Posted: Mon Jul 17, 2017 12:46 pm
by MaxV
Re: Setting the text style of certain words in a field
Posted: Sun May 02, 2021 3:39 pm
by marksmithhfx
dunbarx wrote: ↑Thu Jul 13, 2017 1:50 pm
But just to get back to basics, put some numbers and ordinary words in a field. Put this in a button script somewhere:
Code: Select all
on mouseUp
repeat with y = 1 to the number of words of fld 1
if word y of fld 1 is a number then set the textColor of word y of fld 1 to "red"
else set the textColor of word y of fld 1 to "blue"
end repeat
end mouseUp
Hi Craig, one could quibble, but when I put the following text into a field and ran the code above...
How do you style some text in a field? One, two, 3, 4, who are we for? 100 times 100 is 10000.
I expected all the numbers to turn red. Only 100 and 10000 did. Note 10000 is also followed by punctuation, as is 3 and 4, but 3 and 4 were ignored. I am sure some complicated logic but a numbers a number. If it captures 10000 why not 3 and 4?
Mark
Re: Setting the text style of certain words in a field
Posted: Sun May 02, 2021 4:00 pm
by FourthWorld
You'll want to use the trueWord chunk type rather than word. TrueWord is both Unicode-aware and uses natural-language rules to account for punctuation and other details, where word (for compatibility with older dialects) only uses simple space boundaries.
I've been looking for some Python examples I might use to run comparative benchmarks. This task may be a good fit. How would you do this in Python?