Page 1 of 1
put text into a particular part of a field
Posted: Sat Jul 16, 2011 6:36 pm
by rumplestiltskin
I'm sure I'm just suffering from situational blindness.
I have a locked field that responds to a mouseUp message by returning the clickText and the clickChunk so I know what word was clicked and
where the word is (char 10 to 20 of field 1, for example). Knowing this information, how do I programmatically replace that word with another of my choosing in my script?
Example:
Field 1 contains: The quick brown fox.
I click on the word "brown".
I put the clickChunk into a variable named whereFrom.
I know what I want to replace that word with (let's say "green").
If I use
then that only changes the contents of that variable.
Obviously, I'm missing a line of code but I'm having trouble finding this topic in the documentation. Any help would be appreciated.
Thanks,
Barry
Re: put text into a particular part of a field
Posted: Sat Jul 16, 2011 7:31 pm
by townsend
This works:
Code: Select all
on mouseUp
put "The quick brown fox" into temp
set itemDel to space
put "green" into word three of temp
answer temp
end mouseUp
And here's the stack I always reference, when I have chunk (text manipulation) questions.
Re: put text into a particular part of a field
Posted: Sat Jul 16, 2011 9:09 pm
by SparkOut
That works - but you have no need to set the itemDel to space for it to work.
From the docs: A word is delimited by one or more spaces, tabs, or returns, or enclosed by double quotes. A single word can contain multiple characters and multiple items, but not multiple lines.
LC can happily recognise words separated by spaces as "words" (as opposed to "items") without any adjustment of the itemDel.
Re: put text into a particular part of a field
Posted: Sat Jul 16, 2011 9:25 pm
by rumplestiltskin
Thanks very much for your help. After I originally posted I tried fooling around some more and came up with something that worked:
Code: Select all
put the clickChunk into whereFrom
put empty into char (word 2 of whereFrom) to (word 4 of whereFrom) of me
put "green" after char (word 2 of whereFrom) -1 of me
(the "-1" in the last line of code positions the replacement word so I don't have an extra space before it)
Your code assumes I know which word the user clicked upon. I won't know that until runtime. I have to retrieve that word and its position when the user does click on a word. The user is given a choice of words to put in place of the one he clicked on (so my "green" in the code above would be a variable containing the replacement word the user chose).
Example:
"The quick brown fox." is in field 1. The user clicks on one of the words and my script retrieves the word and its position (and here I assume I have to use the clickText and clickChunk functions unless there's something like clickChunk that returns which
word (it's position in the field) rather than the range of chars). The user then is provided a list of words to substitute for the originally chosen word. He selects it and that word is then put into place. However, because I only know the character range (not which word it is) I'm doing the calculations as shown in my code.
Of course, I could be making this much more difficult that I have to and I'm always open to suggestions!

Re: put text into a particular part of a field
Posted: Sat Jul 16, 2011 11:21 pm
by jmburnod
Hi rumplestiltskin,
For me your code work, if you need the num of the clicked word you can try this script
Code: Select all
on mouseup
put the clickChunk into whereFrom
put "green" into char (word 2 of whereFrom) to (word 4 of whereFrom) of me --•• "Fox." -> "green." work well
--•• If you need the number of the clicked word
put the num of words of char 1 to (word 4 of whereFrom) of me into nbW --••the num of the clicked word
--•• BUT it don't make the job for words with Punctuation
--•• the last word = "fox." and char (word 2 of whereFrom) to (word 4 of whereFrom) of me = "fox"
--put "green" into word nbW of me --•• dont work for words with Punctuation "Fox." -> "green"
answer "You cliqued word"&& nbW&& "of me"
end mouseup
end mouseup
Best regards
Jean-Marc
Re: put text into a particular part of a field
Posted: Sat Jul 16, 2011 11:46 pm
by rumplestiltskin
A number of very nice responses provided ideas for doing this "replace the word" handler. This seems to take care of it in not many lines of code and takes into account a period at the end of a sentence:
Code: Select all
on mouseUp
put the clickChunk into whereFrom
put the num of words of char 1 to (word 4 of whereFrom) of me into theWord
ask "Replace"&& quote & word theWord of me & quote && "with what?"
if it is not empty then
if the last char of word theWord of me is "." then
put it & "." into word theWord of me
else
put it into word theWord of me
end if
end if
end mouseUp
Just have to look for other punctuation possibilities and handle them accordingly. Of course, if the user sees that a punctuation mark is at the end of the clickText (and, in my handler, it does that in the "ask" dialog), then he can simply type it in and I can leave out looking for the punctuation mark as the user will type it in himself. It's an acceptable requirement, I think.
I'm making progress. Thank you all.

Re: put text into a particular part of a field
Posted: Sun Jul 17, 2011 5:57 pm
by jmburnod
Hi rumplestiltskin ,
This seems to take care of it in not many lines of code and takes into account a period at the end of a sentence:
I'm confused, i understood you need the num (position) of the clicked word for other tasks.
I think using the clickchunk is better because there is not punctuation problem.
Be careful. With your new scrip if you type "fox." on the ask dialog the last word of the sentences = "fox.."
Best
Jean-Marc