AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Paste plain text (and copy/cut)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Laszlo



Joined: 14 Feb 2005
Posts: 4031
Location: Pittsburgh

PostPosted: Sat Jul 29, 2006 3:20 pm    Post subject: Paste plain text (and copy/cut) Reply with quote

The functionality of this simple script is there in a few clipboard management scripts already posted (and in a few freeware utilities, like PureText), but if you don't need other fancy features, this one is short and fast. (The simple method Chris described here often fails.)
Code:
^#v::                            ; Text–only paste from ClipBoard
   Clip0 = %ClipBoardAll%
   ClipBoard = %ClipBoard%       ; Convert to text
   Send ^v                       ; For best compatibility: SendPlay
   Sleep 50                      ; Don't change clipboard while it is pasted! (Sleep > 0)
   ClipBoard = %Clip0%           ; Restore original ClipBoard
   VarSetCapacity(Clip0, 0)      ; Free memory
Return
Ctrl-Win-V pastes the text content of the clipboard, leaving it unchanged, so Ctrl-V would still paste formatted text with pictures, if it was in the Clipboard. It is very useful if you paste text from websites into MS Word or Lotus Notes, where they look weird with unusual indentation, font, color, etc. Sleep 50 was necessary in my PC, because setting up the clipboard happens in the background, parallel to the script. If it was not finished when Ctrl-V is sent, the old content was used. Experiment with other values. Sleep 250 should be enough even for the slowest PC's.

If you know, you only need the text content of the selection, but many times, you could save time and memory if the clipboard is converted to text:
Code:
^#x::
^#c::                            ; Text-only cut/copy to ClipBoard
   Clip0 = %ClipBoardAll%
   ClipBoard =
   StringRight x,A_ThisHotKey,1  ; C or X
   Send ^%x%                     ; For best compatibility: SendPlay
   ClipWait 2                    ; Wait for text, up to 2s
   If ErrorLevel
      ClipBoard = %Clip0%        ; Restore original ClipBoard
   Else
      ClipBoard = %ClipBoard%    ; Convert to text
   VarSetCapacity(Clip0, 0)      ; Free memory
Return
Ctrl-Win-C/X copies/cuts the selection to the clipboard and converts it to text.
Back to top
View user's profile Send private message
keybored



Joined: 18 Jun 2006
Posts: 94
Location: Phoenix, AZ

PostPosted: Mon Jul 31, 2006 2:42 am    Post subject: Thanks Lazlo! Reply with quote

This is going to be great. Similar to Skrommels PlainPaste, but the code is easier for me to understand and will fit nicely in my ini file. I'm going to be using this starting tonight.
Back to top
View user's profile Send private message
jak



Joined: 28 Feb 2006
Posts: 102

PostPosted: Wed May 09, 2007 7:41 am    Post subject: Reply with quote

very nice, useful, and elegant. simple too. I'm using it already. perfect.
By the way what I was looking for was the converter to plain text on the 'copy/cut' keys (rather than on the paste keys). Most such converters do it on the paste key. So your second solution was especially perfect for me.
I needed it that way because of a particular program I use where I have to use a menu command to paste, so it had to be converted on the front end...
Back to top
View user's profile Send private message
atnbueno



Joined: 24 Mar 2007
Posts: 26

PostPosted: Wed May 09, 2007 9:48 am    Post subject: Reply with quote

Excellent!

Already in my AutoHotkey.ahk Very Happy
_________________
Regards,
Antonio
Back to top
View user's profile Send private message Visit poster's website
AHKPNH



Joined: 17 Feb 2006
Posts: 35

PostPosted: Wed May 09, 2007 10:20 pm    Post subject: Reply with quote

Very good!!! Razz
Back to top
View user's profile Send private message
jak



Joined: 28 Feb 2006
Posts: 102

PostPosted: Sat Dec 01, 2007 10:47 pm    Post subject: Reply with quote

hi - i've been using lazlo's plain-text-copy script for a while, and its been great. Recently I tried to map the hotkey to the mouse, and I ran into trouble. Wondering whats going on.

The script i'm using is this:

Code:
lwin & c::                            ; Text-only cut/copy to ClipBoard
   Clip0 = %ClipBoardAll%
   ClipBoard =
   StringRight x,A_ThisHotKey,1  ; C or X
   Send ^%x%                     ; For best compatibility: SendPlay
   ClipWait 2                    ; Wait for text, up to 2s
   If ErrorLevel
      ClipBoard = %Clip0%        ; Restore original ClipBoard
   Else
      ClipBoard = %ClipBoard%    ; Convert to text
   VarSetCapacity(Clip0, 0)      ; Free memory
Return


all i did was change the hotkey to:

lwin & lbutton::

When I open notepad and try to copy some text using lwin & lbutton, windows tries to close notepad (I get the "would you like to save your work?" prompt).

any ideas why i'm not able to map this to a mouse button? I've also tried variations like "lctrl & rbutton". It only seems happy if the hotkey is limited to the keyboard, not the mouse.

thanks!

(I should add that a line like "lwin & rbutton::send ^v" works fine for pasting, even when mapped to the mouse like that).
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2589
Location: Australia, Qld

PostPosted: Sun Dec 02, 2007 1:33 am    Post subject: Reply with quote

Change
Code:
   StringRight x,A_ThisHotKey,1  ; C or X
   Send ^%x%                     ; For best compatibility: SendPlay
to
Code:
   Send ^c
otherwise it sends Ctrl+N. Laughing
Back to top
View user's profile Send private message
jak



Joined: 28 Feb 2006
Posts: 102

PostPosted: Sun Dec 02, 2007 1:46 am    Post subject: Reply with quote

THanks! works perfectly.

why did it send control-N? because "lbutton" ends with an "N"?
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 2589
Location: Australia, Qld

PostPosted: Sun Dec 02, 2007 4:12 am    Post subject: Reply with quote

Yes.
Back to top
View user's profile Send private message
sashabe



Joined: 09 Oct 2006
Posts: 14

PostPosted: Thu Dec 27, 2007 11:52 pm    Post subject: Reply with quote

Is it possible to modify this script so that he'd work with Unicode characters? Now, for example, if cyrillic characters are copied, trash letters like "ââèäó ñïåöèôèê" are pasted.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4031
Location: Pittsburgh

PostPosted: Fri Dec 28, 2007 1:16 am    Post subject: Reply with quote

If you want to remove formatting from Unicode text use this:
Code:
^#v::                            ; Text–only paste from ClipBoard
   Clip0 = %ClipBoardAll%
   Transform UC, Unicode         ; Save Unicode text
   Transform Clipboard, Unicode, %UC%
   Send ^v                       ; For best compatibility: SendPlay
   Sleep 50                      ; Don't change clipboard while it is pasted! (Sleep > 0)
   ClipBoard = %Clip0%           ; Restore original ClipBoard
   VarSetCapacity(Clip0, 0)      ; Free memory
Return
It does not work if the Clipboard contains pictures (too).
Back to top
View user's profile Send private message
instantrunoff



Joined: 13 Jan 2008
Posts: 81

PostPosted: Fri May 16, 2008 6:10 am    Post subject: Reply with quote

I have a modified version to remove white space and new line marks and that avoids "pasting" because that can trigger unnecessary paste options in Microsoft Office:

Code:
$>!v:: ; paste without formatting
  gosub getplain
  Send {Raw}%clipboardt%
  clipboardt =
  return
 
  getplain:
  StringReplace, clipboardt, clipboard, `r`n, %A_Space%, All
  clipboardt := RegExReplace(clipboardt, "` {2,}", "` ")
  StringLeft, 1st, clipboardt, 1
  IfInString, 1st, %A_Space%
  StringTrimLeft, clipboardt, clipboardt, 1
  StringRIght, last, clipboardt, 1
  IfInString, last, %A_Space%
  StringTrimRight, clipboardt, clipboardt, 1
  return
 


Updates May 16, 2008


Last edited by instantrunoff on Fri May 16, 2008 6:54 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
sashabe



Joined: 09 Oct 2006
Posts: 14

PostPosted: Fri May 16, 2008 9:33 am    Post subject: Reply with quote

instantrunoff wrote:
I have a modified version to remove white space and new line marks and that avoids "pasting" because that can trigger unnecessary paste options in Microsoft Office:

Code:
   >!v:: ; paste without formatting
     plainpaste:
     StringReplace, clipboardt, clipboard, `r`n, %A_Space%, All
     clipboardt := RegExReplace(clipboardt, "` {2,}", "` ")
     StringLeft, 1st, clipboardt, 1
     IfInString, 1st, %A_Space%
     StringTrimLeft, clipboardt, clipboardt, 1
     Send %clipboardt%
     clipboardt =
     return


Thanks! It finally works with MS Office, but rather than paste instantly, it "types" the clipboard, and therefore is not for large text fragments.
Back to top
View user's profile Send private message
instantrunoff



Joined: 13 Jan 2008
Posts: 81

PostPosted: Fri May 16, 2008 2:33 pm    Post subject: Reply with quote

sashabe wrote:
instantrunoff wrote:
I have a modified version to remove white space and new line marks and that avoids "pasting" because that can trigger unnecessary paste options in Microsoft Office:

Code:
   >!v:: ; paste without formatting
     plainpaste:
     StringReplace, clipboardt, clipboard, `r`n, %A_Space%, All
     clipboardt := RegExReplace(clipboardt, "` {2,}", "` ")
     StringLeft, 1st, clipboardt, 1
     IfInString, 1st, %A_Space%
     StringTrimLeft, clipboardt, clipboardt, 1
     Send %clipboardt%
     clipboardt =
     return


Thanks! It finally works with MS Office, but rather than paste instantly, it "types" the clipboard, and therefore is not for large text fragments.


Yeah, I had it "type" the clipboard rather than paste, because it annoyed me to have paste options or automatic formatting applied when pasting. However, you can use SendPlay for something paste-like.
Back to top
View user's profile Send private message Visit poster's website
instantrunoff



Joined: 13 Jan 2008
Posts: 81

PostPosted: Fri May 16, 2008 6:22 pm    Post subject: Reply with quote

PS I updated the script to prevent issues sending text with characters that get escaped.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group