read file line bny line and sendinput on key press

Get help with using AutoHotkey and its commands and hotkeys
justhere4help
Posts: 1
Joined: 31 Jan 2019, 06:36

read file line bny line and sendinput on key press

31 Jan 2019, 06:41

I am trying to read each line from the file split.txt and input that line whenever I press Ctrl+B. This is my current code and I am getting the error "A return must be incurred prior to the }". Would appreciate some help.

Code: Select all

Loop, Read, split.txt
{
   total_lines = %A_Index%
}

Loop
{
    FileReadLine, line, split.txt, %A_Index%
    if ErrorLevel
        break
    ^b::
  		SendInput, %line%
  	if %A_Index% = total_lines
  		Return
}
AviationGuy
Posts: 185
Joined: 17 Jan 2019, 10:13

Re: read file line bny line and sendinput on key press

31 Jan 2019, 10:31

Hi!

The problem is that you have your hotkey in your loop. Place it before your script like this.

Code: Select all

^b::
Loop, Read, text.txt
{
   total_lines = %A_Index%
}

Loop
{
    FileReadLine, line, text.txt, %A_Index%
    if ErrorLevel
        break
  		SendInput, %line%{space}
  	if %A_Index% = total_lines
  		Return
}
I also added a {space} after the text of each line, otherwise it would place all the text of the lines right after each other (remove the {space} and you'll see what I mean). You can remove it when you don't need it :)
User avatar
Cuadrix
Posts: 199
Joined: 07 May 2017, 08:26

Re: read file line bny line and sendinput on key press

31 Jan 2019, 13:41

This is a better way:

Code: Select all

FileRead, Content, split.txt

i++ := 0

^b::
Loop, Parse, Content, `r,`n
{
	If (A_Index == i) {
		SendInput % A_LoopField
		i++
		Break
	}
}
----

Return to “Ask For Help”

Who is online

Users browsing this forum: Bastiannn, Google [Bot], MannyKSoSo, niczoom, Odlanir, treflip and 206 guests