How best to read in a whole file and output to clipboard?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
omar
Posts: 540
Joined: 22 Oct 2015, 17:56

How best to read in a whole file and output to clipboard?

14 Dec 2017, 09:36

I want to read in a text file with 6000 - 10000 characters.
And then output into the clipboard so I can press ctrl v.

I posted another question about how to store chars like quotes in a variable... But I thought this way will be quicker and easier.

I want to avoid reading line by line and outputting - this will be much slower if say 200 lines.
+ in some cases all 10000 chars are on one line

Thanks
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: How best to read in a whole file and output to clipboard?

14 Dec 2017, 10:26

Code: Select all

^+P::
	KeyWait, Ctrl
	KeyWait, Shift
	FileObj := FileOpen(FileToRead, "r")
	Data := FileObj.Read()
	Clipboard := Data
	ClipWait, 10
	SendInput, ^p
	Return
This might work

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How best to read in a whole file and output to clipboard?

14 Dec 2017, 13:30

- I have two ideas for this, one using WM_CHAR, which can be a slightly slow for thousands of characters, the other using the clipboard. Both use EditPaste for Edit controls which avoids using the clipboard.
- What program are you using? In case there are better ways than these, e.g. Internet Explorer, Excel and Word have COM.
- Btw if anyone knows. Does EditPaste work reliably on RichEdit controls?

Code: Select all

q:: ;send text from file via WM_CHAR (use EditPaste for Edit controls)
WinGet, hWnd, ID, A
vPath := A_Desktop "\MyFile.txt"
FileRead, vText, % vPath

ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
ControlGet, hCtl, Hwnd,, % vCtlClassNN, % "ahk_id " hWnd
if (RegExReplace(vCtlClassNN, "\d") = "Edit")
{
	Control, EditPaste, % vText, Edit1, % "ahk_id " hWnd
	return
}
vText := StrReplace(vText, "`r`n", "`n") ;CRLFs would send 2 enters per line break instead of 1
if hCtl
	hWnd := hCtl
Loop, Parse, vText
	PostMessage, 0x102, % Ord(A_LoopField), 1,, % "ahk_id " hWnd ;WM_CHAR := 0x102
return

w:: ;paste text from file (use EditPaste for Edit controls)
vPath := A_Desktop "\MyFile.txt"
FileRead, vText, % vPath
JEE_SetSelectedText(vText)
return

JEE_SetSelectedText(vText, vWait:=3)
{
	;based on ClipPaste by ObiWanKenobi
	;Robust copy and paste routine (function) - Scripts and Functions - AutoHotkey Community
	;https://autohotkey.com/board/topic/111817-robust-copy-and-paste-routine-function/

	WinGet, hWnd, ID, A
	ControlGetFocus, vCtlClassNN, % "ahk_id " hWnd
	if (RegExReplace(vCtlClassNN, "\d") = "Edit")
		Control, EditPaste, % vText, Edit1, % "ahk_id " hWnd
	else
	{
		ClipSaved := ClipboardAll
		Clipboard := vText
		SendInput, {Shift Down}{Shift Up}{Ctrl Down}{vk56sc02F Down}
		;'PasteWait'
		vWait *= 1000
		vStartTime := A_TickCount
		Sleep, 100
	        while (DllCall("GetOpenClipboardWindow", Ptr) && (A_TickCount-vStartTime < vWait))
			Sleep, 100
		SendInput, {vk56sc02F Up}{Ctrl Up}
		Clipboard := ClipSaved
		ClipSaved := ""
	}
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
omar
Posts: 540
Joined: 22 Oct 2015, 17:56

Re: How best to read in a whole file and output to clipboard?

26 Dec 2017, 22:17

guys thanks for both awesome replies.

all of the code you guys suggested went way above my head!

i tried this in the end:

Code: Select all

#F9::

FileRead, OutputVar, C:\Users\test\Dir Name 1\Dir Name 2\Tests\filereadtext.txt

;MsgBox % OutputVar
Clipboard := OutputVar

SendInput ^v

Return
This works like magic and super fast
any problems with the above?

What I dont get is why there are no problems with illegal characters contained in the file.
I'm not complaining!
Just wondering. :)
lexikos
Posts: 9583
Joined: 30 Sep 2013, 04:07
Contact:

Re: How best to read in a whole file and output to clipboard?

30 Dec 2017, 20:56

What I dont get is why there are no problems with illegal characters contained in the file.
I guess you have previously enclosed a variable name in percent signs and received an error message about illegal characters.

Code: Select all

OutputVar := "<The contents of your variable.>"
Clipboard := %OutputVar%
Error: The following variable name contains an illegal character: "<The contents of your variable.>"
You get this message only when you perform an invalid double-deref. The clue is in bold above: the contents of your variable are being interpreted as the name of another variable.

FAQ: When exactly are variable names enclosed in percent signs?
omar
Posts: 540
Joined: 22 Oct 2015, 17:56

Re: How best to read in a whole file and output to clipboard?

31 Dec 2017, 00:08

@lexikos... thanks for the reply
what i'm saying is that i get the error when i try to store the string directly typed in....
i get this...
but then... i declare a variable and read the contents of a file - give it the exact SAME contents that gave me an error...
and my variable is fine

so confused still

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 316 guests