Autocopy selected text and auto paste in word

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
gregster
Posts: 8921
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: 8921
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: 8921
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
User avatar
FanaticGuru
Posts: 1906
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
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
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 ?
User avatar
FanaticGuru
Posts: 1906
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
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
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
User avatar
FanaticGuru
Posts: 1906
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
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
jakkaas
Posts: 14
Joined: 13 Jul 2021, 01:51

Re: Autocopy selected text and auto paste in word

13 Jul 2021, 02:27

FanaticGuru wrote:
23 Feb 2018, 14:49
djasi78 wrote:I want to use Microsoft Word for saving.
Here is an example using Microsoft Word.

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
/::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(Clipboard "`n")
return

.::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListIndent
	wdApp.Selection.TypeText(Clipboard "`n")
return

,::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListOutdent
	wdApp.Selection.TypeText(Clipboard "`n")
return
#If
You have to start the script and MS Word has to be open.

Then you hit F12 to toggle the script ON.

Then you highlight something and hit / to copy something to Word at the current indent level.
. to copy increasing the indent (this is the ">" key without shift)
, to copy decreasing the indent (this is the "<" key without shift)

Tested hardly any, but seems to work.

FG


Any way to use this script on ONENOTE 16 (not uwp one)?

Thanks
User avatar
submeg
Posts: 326
Joined: 14 Apr 2017, 20:39
Contact:

Re: Autocopy selected text and auto paste in word

13 Jul 2021, 04:52

djasi78 wrote:
23 Feb 2018, 00:33
Hi
I am a medical student and I am working on research project and i need to copy , paste a lot of text from web , pdfs . :|
Although it doesn't meet the requirements of a single key for copy / paste and it doesn't put it into a numbered list, you may come across PDFs that you cannot directly copy the text. In this case, I would recommend Vis2; I've been using it and found it quite useful!
____________________________________
Check out my site, submeg.com
Connect with me on LinkedIn
Courses on AutoHotkey :ugeek:
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Autocopy selected text and auto paste in word

13 Jul 2021, 14:27

jakkaas wrote:
13 Jul 2021, 02:27
FanaticGuru wrote:
23 Feb 2018, 14:49
djasi78 wrote:I want to use Microsoft Word for saving.
Here is an example using Microsoft Word.

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
/::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(Clipboard "`n")
return

.::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListIndent
	wdApp.Selection.TypeText(Clipboard "`n")
return

,::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paragraphs(1).Range.ListFormat.ListOutdent
	wdApp.Selection.TypeText(Clipboard "`n")
return
#If
You have to start the script and MS Word has to be open.

Then you hit F12 to toggle the script ON.

Then you highlight something and hit / to copy something to Word at the current indent level.
. to copy increasing the indent (this is the ">" key without shift)
, to copy decreasing the indent (this is the "<" key without shift)

Tested hardly any, but seems to work.

FG


Any way to use this script on ONENOTE 16 (not uwp one)?

Thanks

Probably but OneNote API is very different from the Word API and I am not familiar with the OneNote API. All the Office suite programs like Word, Excel, Outlook, Access, PowerPoint, etc. all share a common API structure. They are all very similar mainly because each program allows Macros written in VBA which basically tap in to this underlying API interface, so the API was designed with Macros in mind. OneNote does not support Macros, so additional effort and documentation was not put into accessing the API easily.

So yes, this script could probably be adapted for OneNote, but not by me.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
jakkaas
Posts: 14
Joined: 13 Jul 2021, 01:51

Re: Autocopy selected text and auto paste in word

07 Feb 2022, 22:12

Hello. I am using this script below.

Code: Select all

F12::
	if (Toggle = "")
	{
		wdApp := ComObjActive("Word.Application")
	}
	Toggle := !Toggle
	MsgBox % Toggle ? "Clip to Word: ON" : "Clip to Word: OFF"
return
#if Toggle
^!8::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(Clipboard "`n")
return

The issue is that it removes the formatting from the text and then paste it as plain text.

What I what is that when it is sending text to the word, it should be pasted as rich text/unformatted.

Any way I can do that?
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Autocopy selected text and auto paste in word

08 Feb 2022, 19:07

jakkaas wrote:
07 Feb 2022, 22:12
What I what is that when it is sending text to the word, it should be pasted as rich text/unformatted.

Code: Select all

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

#if Toggle
^!8::
	Send ^c
	ClipWait, 1
	wdApp.Selection.Paste
return

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
LAPIII
Posts: 667
Joined: 01 Aug 2021, 06:01

Re: Autocopy selected text and auto paste in word

02 Mar 2022, 07:00

Here is my script for automatically paste you into Notepad++:

Code: Select all

#SingleInstance, Force
#Persistent
OnClipboardChange("ClipChanged")
return
ClipChanged() {
	ControlSend, Scintilla1, ^v`n, ahk_exe Notepad++.exe
}
return
Esc::
MsgBox BYE
ExitApp
Use it you just copy texts and it will automatically be pasted into Notepad++ on a new line. To stop it, press Escape and then to confirm that then a message box will appear. If you want to use any other app, just put the Control in place of Scintilla1. You can get the control with Windows Spy:

Image

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: tiska and 113 guests