[TIP] How to ensure plain pasted text

Mark Schonewille m.schonewille at economy-x-talk.com
Fri Oct 24 06:05:23 EDT 2008


Hi Hugh,

What about this:

on commandKeyDown theKey
   if theKey is "V" and the selectedField is not empty then
     lock screen
     paste
     put the text of the selectedField into the selectedField
     unlock screen
   else
     pass commandKeyDown
   end if
end commandKeyDown

(I haven't tested it)

--
Best regards,

Mark Schonewille

Economy-x-Talk Consulting and Software Engineering
http://economy-x-talk.com
http://www.salery.biz
Dutch forum: http://runrev.info/rrforum/

Benefit from our inexpensive hosting services. See http://economy-x-talk.com/server.html 
  for more info.

On 24 okt 2008, at 11:24, Hugh Senior wrote:

> A little something to keep in your Scripter's Scrapbook for when you  
> need
> it... How do we ensure plain text when we copy and paste from another
> program?
>
> Pasting text from a Browser or an email often results in an unwanted  
> text
> format that doesn't match the field's default style. This is  
> annoying when
> all we want is the data not the style, such as a database field.
>
> The simplest way is to change the clipboardData which is self- 
> managing...
>
> on commandkeyDown pKey
>  if pKey="V" then
>    set the clipboardData["HTML"] to the clipboardData["TEXT"]
>    paste
>  else pass commandkeyDown
> end commandkeyDown
>
> This replaces the clipboard with plain text. It also removes any  
> carriage
> returns so all the text is now on one line. Good if the field is a  
> one-liner
> like a phone number; not so good if we want to paste an address.
>
> So how do we keep paragraphs intact? This is a solution that uses a
> powerful, but I suspect often overlooked, feature of Rev: The  
> ability to
> define default object properties using templates. Rewriting the  
> above we can
> use...
>
> on commandkeyDown pKey
>  if pKey="V" then
>    set the htmlText of the templateField to the clipboardData["HTML"]
>    set the clipboardData["TEXT"] to the text of the templateField
>    reset the templatefield
>    paste
>  else pass commandkeyDown
> end commandkeyDown
>
> We now have plain text on the clipboard including any carriage  
> returns so
> any paragraph formatting is still there. All we need to do is allow  
> for that
> one-liner phone number field by checking for the field's autotab  
> property
> (which we would need to have set anyway)...
>
> on commandkeyDown pKey
>  if pKey="V" then
>    set the htmlText of the templateField to the clipboardData["HTML"]
>    --| Check for one-liner fields and remove carriage returns...
>    if the autoTab of the selectedField then
>      get the text of the templateField
>      replace RETURN with SPACE in it
>      set the text of the templateField to it
>    end if
>    --|
>    set the clipboardData["TEXT"] to the text of the templateField
>    reset the templatefield
>    paste
>  else pass commandkeyDown
> end commandkeyDown
>
> That's it. When we paste text, the original clipboardData is stored  
> in the
> templatefield, which we modified to plain text, then carriage  
> returns are
> removed if necessary, before updating the clipboardData ready for the
> 'paste'.
>
> For the finishing touches, we should include a couple of tests to  
> make sure
> it behaves as expected...
>
> on commandKeyDown pKey
>  if pKey = "V" then
>    --| Check if the environment is appropriate...
>    if the tool is NOT "browse tool" then pass commandKeyDown
>    if the selectedField is empty then pass commandKeyDown
>    if the clipboard is NOT "text" then pass commandKeyDown
>    --|
>    set the htmlText of the templateField to the clipboardData["HTML"]
>    if the autoTab of the selectedField then
>      get the text of the templateField
>      replace RETURN with SPACE in it
>      set the text of the templateField to it
>    end if
>    set the clipboardData["TEXT"] to the text of the templateField
>    reset the templatefield
>    paste
>  else pass commandKeyDown
> end commandKeyDown
>
> A final tweak:
> So far we are assuming that every field is going to be plain text.  
> If we
> want certain fields only, then we might add a field property called
> plainTextOnly, and include a test such as...
>
> on commandKeyDown pKey
>  if pKey = "V" then
>    if the tool is NOT "browse tool" then pass commandKeyDown
>    if the selectedField is empty then pass commandKeyDown
>    if the clipboard is NOT "text" then pass commandKeyDown
>    --| Check if plain text is required...
>    if the plainTextOnly of the selectedField is "TRUE" then
>      set the htmlText of the templateField to the  
> clipboardData["HTML"]
>      if the autoTab of the selectedField then
>        get the text of the templateField
>        replace RETURN with SPACE in it
>        set the text of the templateField to it
>      end if
>      set the clipboardData["TEXT"] to the text of the templateField
>      reset the templatefield
>    end if
>    paste
>  else pass commandKeyDown
> end commandKeyDown
>
>
>
> I hope you will find this helpful.




More information about the use-livecode mailing list