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
How best to read in a whole file and output to clipboard?
- Delta Pythagorean
- Posts: 626
- 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?
Code: Select all
^+P::
KeyWait, Ctrl
KeyWait, Shift
FileObj := FileOpen(FileToRead, "r")
Data := FileObj.Read()
Clipboard := Data
ClipWait, 10
SendInput, ^p
Return
[AHK]......: v2.0.6 | 64-bit
[OS].......: Windows 11 | 22H2 (OS Build: 22621.1555)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat
You should consider migrating to v2. Practice with small features first such as learning how to do Guis!
Remember to use [code] ... [/code] for your multi-line scripts for the forums.
Re: How best to read in a whole file and output to clipboard?
- 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?
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How best to read in a whole file and output to clipboard?
guys thanks for both awesome replies.
all of the code you guys suggested went way above my head!
i tried this in the end:
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.
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
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.
Re: How best to read in a whole file and output to clipboard?
I guess you have previously enclosed a variable name in percent signs and received an error message about illegal characters.What I dont get is why there are no problems with illegal characters contained in the file.
Code: Select all
OutputVar := "<The contents of your variable.>"
Clipboard := %OutputVar%
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.Error: The following variable name contains an illegal character: "<The contents of your variable.>"
FAQ: When exactly are variable names enclosed in percent signs?
Re: How best to read in a whole file and output to clipboard?
@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
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
Who is online
Users browsing this forum: inseption86 and 128 guests