[TIP] How to ensure plain pasted text

Hugh Senior h at FlexibleLearning.com
Fri Oct 24 05:24:59 EDT 2008


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.

/H




More information about the Use-livecode mailing list