Insert text from file without using clipboard

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guzappum
Posts: 2
Joined: 19 Mar 2018, 06:45

Insert text from file without using clipboard

19 Mar 2018, 08:40

Hi,

I'm new to the forum, love using AHK and I usually find solutions by searching the forum. Not this sadly, so I would like to ask for help!

I'm using F4 to insert a block of text from a file, which works great:

Code: Select all

F4::
n++
FileRead, clipboard, unformatted-text.txt
Send ^v
Return
What would be perfect if this did not change the clipboard. I could copy something, insert the text with F4 and still have the original clipboard contents to paste wherever I want to.

How could this be done?

I thought this should work but it is flawed:

Code: Select all

F4::
ClipSaved := Clipboard   ; saving current clipboard
Clipboard = "" ; emptying clipboard
n++
FileRead, clipboard, unformatted-text.txt ; putting contents of .txt file in clipboard
ClipWait ; waiting for the clipboard to be ready - this may be unnecessary, or may be illogically placed
Send ^v ; paste
Clipboard := ClipSaved   ; putting original ciplboard contents back
Return
The results are unreliable : If nothing is copied to the clipboard, it inserts the text block well. If something is coped to the clipboard, it randomly(?) inserts either the clipboard contents or the text block. Pressing it multiple time gives different results. (Either the cipboard contents or the block of text.)

My other ideas include:
- Using Send / SendInput {Raw} would not involve the clipboard, but it is much slower than this method: Each character is typed in separately and this is a larger block of text, so it's not really useful for this purpose.
- I thought about using another .txt file as a temporary place to save the clipboard contents. But how can I empty the file at the end of the script, after the contents are back on clipboard? Otherwise the contents of the file will start to pile up.

Can you more experienced gurus help me solve this?

I don't have any coding experience so I'm having a hard tome understanding the manual.

Thanks a lot in advance!
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Insert text from file without using clipboard

19 Mar 2018, 09:07

Hallo,
try:

Code: Select all

F4::
ClipSaved := ClipboardAll   ; saving current clipboard
Clipboard := "" ; emptying clipboard
n++
FileRead, clipboard, unformatted-text.txt ; putting contents of .txt file in clipboard
ClipWait ; waiting for the clipboard to be ready - this may be unnecessary, or may be illogically placed
Send ^v ; paste
Clipboard := ClipSaved   ; putting original ciplboard contents back
Return
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Insert text from file without using clipboard

19 Mar 2018, 10:25

Code: Select all

#SingleInstance, Force

F12::
	FileRead, Content, my.txt
	Send,% Content
	Return
	
F10::ClipBoard := "blablabla"	
F11::MsgBox % ClipBoard
Tested.
Guzappum
Posts: 2
Joined: 19 Mar 2018, 06:45

Re: Insert text from file without using clipboard

20 Mar 2018, 03:58

Hi, Thanks for both answers!
Rohwedder wrote:ClipSaved := ClipboardAll
This is the same as my code except using ClipboardAll instead of Clipboard in this line. Correct?

Tested again, but still produces unreliable results when pressed several times if a larger block of text (whole paragraph) is included in the .txt
BoBo wrote:#SingleInstance, Force
Good idea, tested #SingleInstance, Force and #SingleInstance, Ignore as well but did not solve the problem sadly.
BoBo wrote:Send,% Content
As I wrote in my first message: Send would be great but it is very slow. It sends separate keystrokes which means you have to wait for a whole block of text (paragraph) to be visibly typed in. Too long to be useful.

So no solution yet. Any other ideas?

How would I use a buffer.txt to store clipboard contents and empty the file at the end of the script?

Clipboard usually contains raw text so loss of formatting is not an issue.

Thanks again!
just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Insert text from file without using clipboard

20 Mar 2018, 04:10

Depending on the receiving application you might need to Sleep a bit before you restore the clipboard.
list-nli

Re: Insert text from file without using clipboard

20 Mar 2018, 04:16

No doubt overkill for a "one file only" need but perhaps useful (future readers of this thread):

Lintalist can "insert from text files" (also HTML, RTF for formatted text if need be).
If you have many text files this may be useful as you can assign shortcuts, hotkeys and use a search GUI to find the one you wish to insert.
* Forum thread: https://autohotkey.com/boards/viewtopic.php?f=6&t=3378
* File Plugin: http://lintalist.github.io/#file-plugin
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Insert text from file without using clipboard

20 Mar 2018, 04:28

As I wrote in my first message: Send would be great but it is very slow. It sends separate keystrokes which means you have to wait for a whole block of text (paragraph) to be visibly typed in. Too long to be useful.
Sorry, I've missed that aspect of your request :lolno: :silent:
- I thought about using another .txt file as a temporary place to save the clipboard contents. But how can I empty the file at the end of the script, after the contents are back on clipboard? Otherwise the contents of the file will start to pile up.
What if simply pushing the content to a variable instead of a file?

Code: Select all

#SingleInstance, Force
!c::  ; press Alt+C to copy content to clipboard
    Send, ^c
    ClipWait
    Content .= ClipBoard . "`n"    ; concatinating the clipboards content to a var (you should probably increase the vars capacity => VarCapacity() for that)!
    Return
    
!v::   ; press Alt+V to paste the vars content via the clipboard. To keep/recreate the clipboards current content check out Rohwedder's code above.
   ClipBoard := Content
   ClipWait
   Send, ^v
   ClipBoard := ""
   Return
   
!s::MsgBox % ClipBoard    ; press Alt+S to show content of the clipboard
Not tested.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: Insert text from file without using clipboard

21 Mar 2018, 15:48

Hello.
Try this. ErrorLevel is used to check if any clipboard contains any content.

Code: Select all

F4::
ClipSaved := Clipboard   ; saving current clipboard
Clipboard := "" ; emptying clipboard

FileRead, clipboard, unformatted-text.txt ; putting contents of .txt file in clipboard
sleep 50 ; sometimes a little delay is necessary for reliable function of clipwait
ClipWait, 1 ; after one second, clipwait skips. Maybe this can interfer with your FileRead, used before.
if (ErrorLevel) ; If there is no data in clipboard, Errorlevel contains "1", else "0"
{
	Clipboard := ClipSaved
	return
}
Send ^v ; paste
Clipboard := ClipSaved   ; putting original ciplboard contents back
Return
Einfach nur ein toller Typ. :mrgreen:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: CrowexBR, doodles333, vysmaty and 244 guests