put text into a particular part of a field

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

put text into a particular part of a field

Post by rumplestiltskin » Sat Jul 16, 2011 6:36 pm

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

Code: Select all

put "green" into whereFrom
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

townsend
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 430
Joined: Sun Feb 13, 2011 8:43 pm

Re: put text into a particular part of a field

Post by townsend » Sat Jul 16, 2011 7:31 pm

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.
Attachments
text workshop.zip
(154.14 KiB) Downloaded 300 times

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: put text into a particular part of a field

Post by SparkOut » Sat Jul 16, 2011 9:09 pm

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.

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: put text into a particular part of a field

Post by rumplestiltskin » Sat Jul 16, 2011 9:25 pm

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! :D

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: put text into a particular part of a field

Post by jmburnod » Sat Jul 16, 2011 11:21 pm

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
https://alternatic.ch

rumplestiltskin
Posts: 223
Joined: Wed Jun 21, 2006 7:33 pm
Contact:

Re: put text into a particular part of a field

Post by rumplestiltskin » Sat Jul 16, 2011 11:46 pm

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. :D

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: put text into a particular part of a field

Post by jmburnod » Sun Jul 17, 2011 5:57 pm

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
https://alternatic.ch

Post Reply