Pasting plain text from clipboard + removing line breaks?

Discussion, questions and support.
Post Reply
cadudesun
Posts: 103
Joined: Jun 6th, ’15, 03:28

Post by cadudesun » Jun 9th, ’15, 22:48

Hi,

I found the following AHK syntax which is working amazingly in FastKeys interface in order to paste text without formatting.

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

Does anyone know it there is any way to adjust this AHK syntax (or even any other in-built workaround in FastKeys) to also paste removing line breaks + replacing them with spaces?

It would be handy when copying/pasting text from PDF files.

Many thanks!
User avatar
gustms
Posts: 133
Joined: Sep 26th, ’14, 21:32
Location: USA

Post by gustms » Jun 9th, ’15, 23:25

FastKeys comes with a "Paste without formatting" command that works great for me too. See the library.

Code: Select all

clipboard := clipboard
Send, ^{vk56} ;Ctrl V
But I am very interested as well in that "paste removing line breaks + replacing them with spaces", if it is possible.
User avatar
Marko
Posts: 1730
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jun 9th, ’15, 23:37

This should replace all line breaks in the clipboard with spaces:

Code: Select all

StringReplace, clipboard, clipboard, `r`n, %A_Space%, All
so you can try:

Code: Select all

clipboard := clipboard
StringReplace, clipboard, clipboard, `r`n, %A_Space%, All
Send, ^v
You can add temporary variable to preserve original clipboard (like str_temp in your case).

Also, when using FastKeys Clipboard manager (default Ctrl-Alt-V), you can hold Ctrl key to paste without formatting.
cadudesun
Posts: 103
Joined: Jun 6th, ’15, 03:28

Post by cadudesun » Jun 11th, ’15, 18:08

Thanks for valuable information!

Going a step further, does anyone 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:

Through AHK forum I accessed this code, but it seems not working as expected, or even the problem is in the PDF files formatting I've tested:

Code: Select all

#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
User avatar
Marko
Posts: 1730
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jun 12th, ’15, 18:12

Could you post a link to actual example page/text which you would like to copy/paste and the expected result?
cadudesun
Posts: 103
Joined: Jun 6th, ’15, 03:28

Post by cadudesun » Jun 15th, ’15, 01:55

Hi,

I made available a .PDF sample and a .DOC expected output in the following link:

Actually, I achieved this output by using the function "copy without format" from Adobe Acrobat, and them pasting using the AHK script I posted above. However, if I simple copy and then use the script, the expected output doesn't appear. Since other PDF programs don't have this Acrobat function "copy without format", I would really appreciate to know if there is a workaround to copy (ctrl + v) and then pasting using the AHK script, achieving the expected output (.DOC I made available).

By the way, is there any way to attach files direct in the forum, or should I share content through links?

Thanks for assistance!
User avatar
Marko
Posts: 1730
Joined: Mar 2nd, ’13, 21:02

Post by Marko » Jun 15th, ’15, 12:44

sorry, the link is not working.
Post Reply