Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Pasting plain text from the clipboard


  • Please log in to reply
10 replies to this topic
redgum
  • Members
  • 59 posts
  • Last active: Mar 26 2015 01:30 AM
  • Joined: 07 Dec 2007

Hi,

 

According to the Help for Clipboard,

clipboard = %clipboard%

should convert any copied files, HTML, or other formatted text to plain text.

 

I have a macro that's been using that, designed to paste plain text only from whatever's on the clipboard:

#Insert:: 		;Win-Insert to paste text only; formatted version remains in clipboard
	OriginalClipboardContents = %ClipBoardAll%
	ClipBoard = %ClipBoard%                             ; Convert to text
	Send ^v 						
	Sleep 100                                           ; Don't change clipboard while it is pasted
	ClipBoard := OriginalClipboardContents              ; Restore original clipboard contents
	OriginalClipboardContents =                         ; Free memory
	Return 	

This has worked reliably for me for years, but recently not so much - it's now often (but not always) pasting formatted text, instead of plain.

 

Any idea what might have changed?

 

Thanks,

 

-jim.



axlar
  • Members
  • 196 posts
  • Last active: Dec 04 2015 08:50 AM
  • Joined: 29 Nov 2012

Hi redgum,

 

1. How about convert with some dummy function?

	StringReplace, str_temp, ClipBoard, __blahblahasdfasdf
	ClipBoard := str_temp
	Send ^v 						

 

2. I tested your hotkey, sometimes formatted text was placed.

I figured out that I miss-pressed Shift-Win-Insert. (Shift-Insert will plain paste in Microsoft word)

So I think hotkey "*#Insert" is better...



redgum
  • Members
  • 59 posts
  • Last active: Mar 26 2015 01:30 AM
  • Joined: 07 Dec 2007

Hi axlar,

 

Your suggestion in #1 worked perfectly. Still curious why my original approach no longer works reliably - it was rock solid for years - but at least my macro works again.

 

Re #2 - Shift-Insert works in most programs for formatted paste - it was the precursor to Ctrl-V. I don't want to replace Shift-Insert, as that's what I use to paste formatted - at least on keyboards that still have an Insert key, which is fewer and fewer these days, particularly laptops. Ctrl-Insert to copy, Shift-Insert to paste, Shift-Delete to cut - and Win-Insert to paste unformatted! I find that a lot of the time I'm copying and pasting, I'm also using the Delete, Home and End keys, so keeping my fingers over there is a lot more convenient than jumping back and forth to X, C & V keys.

 

But I digress. Thanks for the help!

 

-jim.



axlar
  • Members
  • 196 posts
  • Last active: Dec 04 2015 08:50 AM
  • Joined: 29 Nov 2012

OK I see. forget about #2.happy.png



redgum
  • Members
  • 59 posts
  • Last active: Mar 26 2015 01:30 AM
  • Joined: 07 Dec 2007

Actually, suggestion 1 (Post 2) hasn't fixed it. Sometimes it pastes formatted, sometimes unformatted...

 

-jim.



axlar
  • Members
  • 196 posts
  • Last active: Dec 04 2015 08:50 AM
  • Joined: 29 Nov 2012

I caught it. On word, 100msec sleep is short. OMG ClipBoard will be reverted during pasting..

Try to enlarge the delay..



redgum
  • Members
  • 59 posts
  • Last active: Mar 26 2015 01:30 AM
  • Joined: 07 Dec 2007

Aha! Bet that's it. Explains why it works sometimes, and not others, too.

 

I've upped it to 250, and will test for a while to see if that fixes it. If so, will see how much I can cut it back while maintaining reliability.

 

Thanks for the help, axlar!



cadudesun
  • Members
  • 24 posts
  • Last active: Jun 04 2016 02:55 PM
  • Joined: 06 Nov 2013

 

 

    StringReplace, str_temp, ClipBoard, __blahblahasdfasdf
    
ClipBoard := str_temp
    
Send ^v     

 

Is there any way to adjust this syntax to also paste removing line breaks?

It would be handy when copy/paste from PDF files.

Thanks!  



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
StringReplace, Clipboard, ClipBoard, `r`n, %A_Space%, All
StringReplace, Clipboard, ClipBoard, `n, %A_Space%, All
Send ^v


cadudesun
  • Members
  • 24 posts
  • Last active: Jun 04 2016 02:55 PM
  • Joined: 06 Nov 2013

Many thanks for the script for removing the line break. It worked wonderfully!

 

Do you know if it is possible to make an adjustment in order to preserve / not remove breaks when there is tabs or hard returns between paragraphs?

 

The idea is to adjust the line breaks inside same paragraph, but when there a new paragraph/session starts, the pasted text could keep some structure from the divisions of the original text. 

 

I made available a text example in the following link: https://drive.google...iew?usp=sharing



space
  • Members
  • 520 posts
  • Last active:
  • Joined: 12 Aug 2014
#v::
; Trim leading/trailing white space from empty lines 
Clipboard:=RegExReplace(Clipboard,"m)^[ \t]*$","`r`n")

; copied from http://ahkscript.org/docs/commands/StringReplace.htm
; Remove all blank lines from the text in a variable:
Loop
 {
   StringReplace, ClipBoard, ClipBoard, `r`n`r`n, --[ahkparagraphmarker]--, UseErrorLevel
   if ErrorLevel = 0  ; No more replacements needed.
     break
 }

;Replace all new lines with a space topreventjoinedwords 
StringReplace, ClipBoard, ClipBoard, `r`n, %A_Space%, All

; Remove all double spaces (useful for justified text)
Loop
 {
    StringReplace, ClipBoard, ClipBoard, %A_Space%%A_Space%, %A_Space%, UseErrorLevel
    if ErrorLevel = 0  ; No more replacements needed.
        break
 }

; re-create paragraphs again
StringReplace, ClipBoard, ClipBoard,--[ahkparagraphmarker]--,`r`n`r`n, All

; remove any leftover remaining leading spaces
Clipboard:=RegExReplace(Clipboard,"m)^[ \t]*")

Send ^v
Return