Page 1 of 1

Color

Posted: Wed Oct 27, 2010 11:32 pm
by p4tr1ck
How would I go about changing the color of particular text in a field? (not all of it)

I looked through the dictionary and it all seemed to reference objects, not chunks.
Thanks.

Re: Color

Posted: Thu Oct 28, 2010 1:07 am
by bn
patrick,

would the textcolor do?

Code: Select all

set the textcolor of word 5 to 9 of field "myField" to red
regards
Bernd

Re: Color

Posted: Thu Oct 28, 2010 2:38 am
by p4tr1ck
Thanks, sorry for asking such a simple question. :)
I can't believe I didn't find that on the dictionary... :roll:

Re: Color

Posted: Thu Oct 28, 2010 3:22 am
by FourthWorld
p4tr1ck wrote:I can't believe I didn't find that on the dictionary... :roll:
Happens to all of us. We think it should be something really cryptic, and then we find out it's almost English.

Almost. Then later we start looking for things expecting them to be really simple, and find some of them to be somewhat cryptic. ;)

Re: Color

Posted: Thu Oct 28, 2010 10:08 pm
by p4tr1ck
I'm having some trouble using the textcolor property. How would I, say, set the selected text of a field to the color red?
It seems that every time I use your code exactly it works fine, but when I try it breaks :?

Code: Select all

set the textcolor of the selectedText to red
I've tried selectedChunk as well. What am I doing wrong?
Thanks for your help. :)

Re: Color

Posted: Thu Oct 28, 2010 10:20 pm
by bn
patrick,
the selectedText gives you the actual text.
You are looking for a chunk expression like "char 30 to 50 of field MyField"
The selectedChunk is what you are looking for.

Code: Select all

on mouseUp
   put the selectedChunk of field 1 into tChunk
   set the textColor of tChunk to red
end mouseUp
(tested)
If you are unshure what a particular keyword does I often code
put the selectedChunk of field 1
which puts the value of the expression into the message box.

regards
Bernd

Re: Color

Posted: Thu Oct 28, 2010 10:30 pm
by p4tr1ck
Thanks, I'm a little rusty on text and chunks, I'll have to read its user guide part again.
Thanks for all the help. :)

Re: Color

Posted: Thu Oct 28, 2010 10:33 pm
by p4tr1ck
Sorry for the double post, but I'm getting some errors.

Code: Select all

on mouseUp
   local tChunk
   put the selectedChunk of field "userCode" into tChunk
   set the textColor of tChunk to red
end mouseUp
That brings up this error:
"button "Button": execution error at line 4 (Chunk: error in object expression), char 22"
I am using the correct field name...
Thanks,
Patrick

Re: Color

Posted: Fri Oct 29, 2010 2:48 am
by Regulae
Hi Patrick,

Something to remember when working with “selectedText” and “selectedChunk” is that clicking the mouse clears the selection, i.e. if I have a button containing the script:

Code: Select all

on mouseUp
   local tChunk
   put the selectedChunk of field "userCode" into tChunk
   set the textColor of tChunk to red
end mouseUp
... as in your example, it runs into the problem that, if I select some text, then click the button, I will by clicking the button clear the selection. If instead you have the script:

Code: Select all

on mouseEnter
   local tChunk
   if the selection is not empty then
      put the selectedChunk into tChunk
      set the textColor of tChunk to red
   end if
end mouseEnter
... select some text, then move the mouse over the button, the selectedChunk will be made red. Three things are worth noticing about the above script:
1. By using “mouseEnter”, the text we selected is still selected when the script runs, changing the textColor as desired.
2. “if the selection is not empty”... we need to put this condition in simply because, as this button uses “mouseEnter” if we happen to move the mouse into it without having selected some text, we would get an error. You could try “commenting out” the condition to see the problem, thus:

Code: Select all

on mouseEnter
   local tChunk
   --   if the selection is not empty then
   put the selectedChunk into tChunk
   set the textColor of tChunk to red
   --end if
end mouseEnter
... and move the mouse into the button, without having selected any text. The problem would be the line:

Code: Select all

set the textColor of tChunk to red
... because, with no text selected, the selectedChunk is empty, thus tChunk is empty, and so Rev is now trying to set the textColor of an unspecified chunk of text to red, which generates an error. This was the problem with your script as well- clicking the button cleared the selection so nothing was selected when the script actually ran.
3. If, for example, there is only one field on the card (hence it is “field 1”) and it contains the single sentence “This is an example”- if you then select the first word:
the selectedText ... is “This”
the selectedChunk ... is “char 1 to 4 of field 1”
so, because the selectedChunk already specifies the field involved (field 1) it is not really necessary, as I understand it, to specify the field explicitly as in:

Code: Select all

the selectedChunk of fld "usercode"
I should also mention that a simplified script would also work:

Code: Select all

on mouseEnter
   if the selection is not empty then
      set the textColor of the selectedChunk to red
   end if
end mouseEnter
I hope this is of some help.

Regards,
Michael

Re: Color

Posted: Fri Oct 29, 2010 3:21 am
by p4tr1ck
It never occurred to me that the selection cleared...Thanks a ton! :)

Re: Color

Posted: Fri Oct 29, 2010 4:07 am
by Regulae
Hi Patrick,

Glad to hear things are working out. Something I should clarify:

Code: Select all

the selectedChunk of field “usercode”
... is perfectly valid, just more specific than:

Code: Select all

the selectedChunk
... on its own. If you have several fields on a card, if you select text in any of them, the selectedChunk will be of the form “char(a) to (b) of field (whichever numbered field text was selected in)”, whereas:

Code: Select all

the selectedChunk of field “usercode”
... will be empty, unless the current selection is specifically in the field “usercode”. In some cases, you may want a script to operate on the selectedChunk regardless of which field the user is working on, so just “the selectedChunk” is very useful. On the other hand, there may well be design situations where as the developer you want things only applicable to a specific field. To be honest, I hadn’t realised you could specify the selectedChunk in this way, but I can well see how it could be very useful. Once again, I learn from my esteemed German colleague.

Regards,
Michael