Page 1 of 1

Clear text formatting

Posted: Wed Mar 06, 2013 2:16 am
by MisMel
Is there a way to clear formatting from a text field after I have pasted formatted text?

If I have a field and I accidentally paste formatted text (for example, if I paste something from Word), then what gets pasted is neither plain text nor text with the original formatting - instead, it is tiny text with the line height of the text size set for that field. After that, there is nothing I can do to change the format of the text. But worse, even after I delete the text from the field and paste unformatted text, it still has the same "tiny text" formatting, no matter what I do to try to change it. Is there a way to change the formatting of the field after formatted text was pasted there?

Better yet, is there a way to change the default paste behavior to "paste unformatted"?

Re: Clear text formatting

Posted: Wed Mar 06, 2013 2:36 am
by Simon
That would be a great feature "paste unformatted".
From Word if you highlight the text then ctrl+spacebar it will remove the formatting, then you copy it. Normally I just copy the whole thing and paste it into Notepad then copy the bits I want from there. I don't know if Textedit retains formatting.

Simon

Re: Clear text formatting

Posted: Wed Mar 06, 2013 2:56 am
by Simon
Ahhh, but I didn't solve your question.
Try this:

Code: Select all

on mouseUp
   repeat with x = 1 to the number of fields on this cd
      set the textFont of field x to""
      set the textSize of  field x to""
      set the textStyle of field x to"plain"
      put the plainText of word 1 to -1 of field x into fld x
   end repeat
end mouseUp
Might be a bit of overkill.

Simon

Re: Clear text formatting

Posted: Wed Mar 06, 2013 8:22 am
by bn
Hi,

in the Edit menu there is an option to paste unformatted text
alt-shift-command V

also
have you tried

Code: Select all

put the text of field "myField" into field "myField"
by putting the text of a field into itself you get rid of the formatting which is in the htmlText when pasting formatted text into a field.

ClipboardData["text"] contains the unformatted text.

Kind regards
Bernd

Re: Clear text formatting

Posted: Wed Mar 06, 2013 11:05 pm
by MisMel
Thanks, all, both of your suggestions allowed me to clear the formatting from the field.

I know that there is an option to paste unformatted text, however when a user is running my app, he will just use command-V to paste, which ruins the formatting of the field. So, it would be nice if I could force it to always paste unformatted, (the way the program used to behave when it was Revolution).

Re: Clear text formatting

Posted: Thu Mar 07, 2013 1:15 am
by bn
Hi MisMel,

if you want this in an app then have a look at "pasteKey" in the dictionary.

During delelopment in the IDE pasting is trapped by the IDE and pasteKey is not sent.
On a field, card or stack script level you can do this during development

Code: Select all

on rawKeyDown pKey
   if the environment <> "development" then pass rawKeyDown
   if pKey = 118 then
      if the commandKey is down or the controlKey is down then
         put the clipboardData["text"] into tText
         if tText <> "" then 
            if the selectedChunk <> "" then
               put tText into the selectedChunk
            end if
         end if
      else
         pass rawKeyDown
      end if
   else
      pass rawKeyDown
   end if
end rawKeyDown
When in your application you can do

Code: Select all

on pasteKey
   put the clipBoardData["text"] into tText
   if tText <> "" then
      put the selectedChunk into tSelected
      if tSelected <> "" then
         put tText into the selectedChunk
      end if
   else
      pass pasteKey
   end if
end pasteKey
you can test pasteKey when developing by selecting Menu Development -> Suspend Development Tools

In testing both scripts briefly it worked. But both would have to be tested thouroghly for unwanted effects.
BTW I tested with formatted text taken from the Script Editor with colorization on. Blocking the rawkeyDown script pasted colorized text, unblocking rawkeyDown plain text. Same when testing pasteKey in suspended development tools.
I had both scripts active during testing both in development and suspend development.

Maybe some of this is helpful for your app.

Kind regards
Bernd

Kind regards
Bernd

Re: Clear text formatting

Posted: Thu Mar 21, 2013 10:21 pm
by MisMel
Bernd,

Thanks for your reply. I am fairly new to LiveCode, so pardon my "beginner" questions, but for the second bit of code you included, for the standalone application, where would I put that code? Would I need to include it for each field, on the card script, or is it something that needs to go in my main script?

On a different but related note, I've noticed that something happened recently (not sure if it's something I did, or if it happened after I updated LiveCode, when the paste issue began). While I can paste into my standalone app using command+V, I cannot copy and I cannot select anything with the keyboard (command+C doesn't work, nor does command+A for select all). Any idea what is happening here?

Melissa

Re: Clear text formatting

Posted: Fri Mar 22, 2013 12:35 am
by bn
Hi Melissa,

for command-A, C, etc to work in a standalone you have to script it. Best would to use the menu builder to start. It can autoscript the menus but you would have to specifically indicate what happens when.

See the documentation about Menus
and here is a very nice article by Jaque regarding the different behaviour of menus Windows vs. Mac. On a Mac it is a little trickier to do the menus.
http://www.hyperactivesw.com/mctutorial ... Menus.html
and
http://www.hyperactivesw.com/mctutorial ... Menus.html

menus are a bit confusing at first but get a thorough understanding of them and experiment with them in small sample stacks -> standalones to get to know them. It will save you some headaches later.

As far as where to place the pasteKey handler: you could put the body of that into the menuPick handler of the Edit menu once you have built your menus. That would be the obvious place. You would not the entire pastKey handler there but just the logic of the handler.

If you dont have menus then you could place the paseKey handler at the stack script level to apply to all cards/substacks.

Kind regards
Bernd

Re: Clear text formatting

Posted: Fri Mar 22, 2013 1:11 am
by bn
Hi Melissa,

if you use the menu builder and have the menu builder auto script the handlers you have to add the logic to the handler.
here is what it would look like in the script

Code: Select all

--The following menuPick handler was generated by the Menu Builder.
on menuPick pWhich
   switch pWhich
      case "Cut"
         --Insert script for Cut menu item here
         break
      case "Copy"
         copy
         break
      case "Paste"
         put the clipBoardData["text"] into tText
         if tText <> "" then
            put the selectedChunk into tSelected
            if tSelected <> "" then
               put tText into the selectedChunk
            end if
         else
            paste
         end if
         break
      case "Clear"
         --Insert script for Clear menu item here
         break
      case "Preferences"
         --Insert script for Preferences menu item here
         break
   end switch
end menuPick
I just added the code for paste and copy.

This works as expected in a standalone: formatted text is pasted as unformatted text other objects are just pasted.

Kind regards
Bernd

Re: Clear text formatting

Posted: Fri Mar 22, 2013 11:07 pm
by MisMel
Bernd, thanks again for your help.

The pastekey script works perfectly.

I needed to make the command+C and command+A shortcuts work without the menu (I checked to confirm, and the shortcuts all work in an app that I created using Revolution 4.0.0.950 but it doesn't work in an app created in Livecode 5.5.3), but fortunately, I found another post in the forums that had the solution. I added the following code to my main stack script and it seems to do the trick:

Code: Select all

on commandKeyDown pKey
   if pKey is "c" then
      copy
   else if pKey is "a" then
      select text of the selectedField
   else
      pass commandKeyDown
   end if
end commandKeyDown