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