Autocopy selected text and auto paste in word

Get help with using AutoHotkey and its commands and hotkeys
gregster
Posts: 4946
Joined: 30 Sep 2013, 06:48

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 06:20

I think that is not generally true, Nwb. Edit: I just checked: Ctrl+c works in unprotected pfds.
It rather depends on the pdf. Not all pdfs allow copying, afaik. I think, some are "secured" by password.
If you can get the text on the clipboard, it shouldn't be a problem to save the text via an AHK script. But I guess that is the point where the whole thing fails... or are you able to copy the text "manually"?

If the pdf restricts copying, you might have to rely on tools that could touch legal questions, depending on the jurisdiction of your country of residence. Perhaps OCR is a (legal) option, if "removing" a password isn't.

Edit2: I see, at least this problem was resolved for now: https://autohotkey.com/boards/viewtopic ... 20#p202337
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 06:42

gregster wrote:I think that is not generally true, Nwb. Edit: I just checked: Ctrl+c works in unprotected pfds.
It rather depends on the pdf. Not all pdfs allow copying, afaik. I think, some are "secured" by password.
If you can get the text on the clipboard, it shouldn't be a problem to save the text via an AHK script. But I guess that is the point where the whole thing fails... or are you able to copy the text "manually"?

If the pdf restricts copying, you might have to rely on tools that could touch legal questions, depending on the jurisdiction of your country of residence. Perhaps OCR is a (legal) option, if "removing" a password isn't.

Edit2: I see, at least this problem was resolved for now: https://autohotkey.com/boards/viewtopic ... 20#p202337
What the duck copying text could have consequences? :crazy:
How in the heck are they going to know though?
I am your average ahk newbie. Just.. a tat more cute. ;)
gregster
Posts: 4946
Joined: 30 Sep 2013, 06:48

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 06:48

Nwb wrote:What the duck copying text could have consequences? :crazy:
I talked about "removing" passwords. Apart from that, it certainly depends on the amount of text and what you do with it, if you could suffer consequences. Scientific citations shouldn't be a problem (you can still copy it by hand, anyway) - but removing copy protection by cracking a pdf might still be illegal. Just saying...
How in the heck are they going to know though?
That is another question and not our concern. But as a forum of a legitimate software that already suffers partly from a reputation of being a cheating software for gamers (and regularly gets false positives with antivirus softwares), we shouldn't endorse removing (or "getting around") copy protections and at least point out potential legal pitfalls. If you then choose to ignore that advice, that is your personal decision.
djasi78
Posts: 27
Joined: 23 Feb 2018, 00:28

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 07:14

it was just a free article , pdf.

Can formatting be disabled in the script ?
gregster
Posts: 4946
Joined: 30 Sep 2013, 06:48

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 07:23

Most probably, but I have no experience with Word's COM interface; and it is not even installed on the computer I'm on at the moment. So, I might have a look later, but let's hope that Fanatic Guru is back soon :D
FanaticGuru
Posts: 1435
Joined: 30 Sep 2013, 22:25

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 13:19

Nwb wrote:What the duck copying text could have consequences? :crazy:
How in the heck are they going to know though?
I find that funny.

Yea, copying text can have consequences. There are lawyers that special in nothing but that. It is called copyright law. It literally has the basics of the law in the name. The right to copy. If you don't have the right to copy something then it is against the law to copy it. If the author enforces their copyright protections then there can be consequences. It is generally against the law to copy any material that is copyrighted by any means. Meaning you cannot use the clipboard to copy and paste, you can not photocopy, take a picture, write it with a stick in the dirt, etc.

Now sure, it is any easy law to break and get away with and most people have broken copyright laws at one time or another. Heck, I probably break some type of copyright or trademark or other intellectual property law almost every day. I just try not to be abusive and be a decent member of the human race. I guess you could take the stance that nothing is illegal unless you get caught.

On the technical side, PDFs have a protocal for protecting copyrights. The author of a PDF can embed in the PDF information about whether a PDF can be copy/pasted, printed, printed at low quality, modified, etc. Now PDF viewers do not have to observe these protocals. Some PDF views will ignore all of these protocals and let you do anything you want with an open PDF. The only fairly secure feature is the password to even open the PDF as these encrypt the file but once it is open the viewer has access to the information and can ignore the other protocals about how the author wanted to limit its usage. It is fairly common in the business world to deal with PDFs that are secured against copying or modifing. You try to copy and paste and it just does not work. You push the keys but nothing happens. For those that do not know about this feature of PDFs it can be fustrating what is going on.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
djasi78
Posts: 27
Joined: 23 Feb 2018, 00:28

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 13:24

Can formatting be disabled in the script ?

Thank you ?
FanaticGuru
Posts: 1435
Joined: 30 Sep 2013, 22:25

Re: Autocopy selected text and auto paste in word

24 Feb 2018, 13:56

djasi78 wrote:Can formatting be disabled in the script ?
It is not exactly formatting. Formatting is already being stripped out. Things like bold, font, size, etc.

What is probably causing the problem is some type of new line character. Each new line character starts a new paragraph in Word which starts a new number.

One bad thing about copy/pasting from a PDF is each line has an implicit new line character at the end. A sentence that wraps on two lines in a PDF will be pasted as two lines regardless of the word wrap of the destination.

So you can try stripping out new line characters, or maybe better change all new line characters to Shift+Enter which is special in Word in that it is a line break but not a new paragraph. This will cause a line to continue below without causing a new number.

Code: Select all

F12::
	if (Toggle = "")
	{
		wdApp := ComObjActive("Word.Application")
		wdApp.Selection.Range.ListFormat.ApplyOutlineNumberDefault
		Toggle := 0
	}
	Toggle := !Toggle
	MsgBox % Toggle ? "Clip to Word: ON" : "Clip to Word: OFF"
return

#if Toggle
Down::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Right::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListIndent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Left::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListOutdent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return
#If
The change is: RegExReplace(Clipboard, "\v+", Chr(11))
This takes the text from the clipboard and replaces all vertical tabs \v with chr(11) which is line break in Word.
Vertical tab is a catch all for many things that can cause vertical spacing which is basically when you move down lines.

Depending on what you are pasting, this might be better: RegExReplace(Clipboard, "\v+")
This would strip out all vertical tabs and replace them with nothing. Then let Word handle all the wrapping.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers
djasi78
Posts: 27
Joined: 23 Feb 2018, 00:28

Re: Autocopy selected text and auto paste in word

25 Feb 2018, 00:59

I am getting this error while running the script and it is not pasting the copied text in word.

Image

Thank you.
djasi78
Posts: 27
Joined: 23 Feb 2018, 00:28

Re: Autocopy selected text and auto paste in word

25 Feb 2018, 01:07

Here , is the full script. I added some code from previously mentioned word code , so that it opens word automatically.

Now, there is no error but it does not paste the selected text. If , i press down arrow on selected text in browser , the page moves down but nothing is pasted in word.

Code: Select all

wdMainTextStory := 1

wdApp := ComObjCreate("Word.Application")   ; Create an instance of Word.
wdApp.Visible := true                       ; Make Word visible.
NotesDoc := wdApp.Documents.Add()           ; Add a new document to Word.
return                                      ; End of auto-execute section.
F12::
	if (Toggle = "")
	{
		wdApp := ComObjActive("Word.Application")
		wdApp.Selection.Range.ListFormat.ApplyOutlineNumberDefault
		Toggle := 0
	}
	Toggle := !Toggle
	MsgBox % Toggle ? "Clip to Word: ON" : "Clip to Word: OFF"
return

#if Toggle
Down::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Right::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListIndent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Left::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListOutdent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return
#If
Esc:: ExitApp
FanaticGuru
Posts: 1435
Joined: 30 Sep 2013, 22:25

Re: Autocopy selected text and auto paste in word

26 Feb 2018, 12:45

djasi78 wrote:Here , is the full script. I added some code from previously mentioned word code , so that it opens word automatically.

Now, there is no error but it does not paste the selected text. If , i press down arrow on selected text in browser , the page moves down but nothing is pasted in word.

Code: Select all

wdMainTextStory := 1

wdApp := ComObjCreate("Word.Application")   ; Create an instance of Word.
wdApp.Visible := true                       ; Make Word visible.
NotesDoc := wdApp.Documents.Add()           ; Add a new document to Word.
return                                      ; End of auto-execute section.
F12::
	if (Toggle = "")
	{
		wdApp := ComObjActive("Word.Application")
		wdApp.Selection.Range.ListFormat.ApplyOutlineNumberDefault
		Toggle := 0
	}
	Toggle := !Toggle
	MsgBox % Toggle ? "Clip to Word: ON" : "Clip to Word: OFF"
return

#if Toggle
Down::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Right::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListIndent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return

Left::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListOutdent
	wdApp.Selection.TypeText(RegExReplace(Clipboard, "\v+", Chr(11)) "`n")
return
#If
Esc:: ExitApp
Your code works fine for me, although it has some dedundant code that is not needed.

These lines are not really needed:

Code: Select all

wdMainTextStory := 1
wdApp := ComObjActive("Word.Application")
Esc:: ExitApp
It sounds like your clipboard is not getting anything on it by doing Control+C. If you can not put stuff manually on the clipboard by pushing Control+C then this script will not work.

Like said before. Control+C will not always put stuff on the clipboard. Whether it does is controlled by the application that is being copied from. Some times it is intentionally blocked other times it is changed to a different key combination.

Also I assumed a basic understanding of AHK that this is designed so that you can toggle it off and on with F12 for when you need to use the arrow keys normally and also Escape ends the script which is normally just for testing purposes and is not something you would see in a normal usage of a script as Escape is a very common key to hit for other reasons.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: DanRim, doubledave22, MannyKSoSo and 38 guests