Text shortcut to load file and read and then make shortcut - doesn't work

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

Text shortcut to load file and read and then make shortcut - doesn't work

Post by omar » 13 Mar 2024, 12:36

I want to make a text short cut to:
- load a text file and read each line into an array.
- Then I want to randomly pick a line and output.

My code doesn't seem to work.
I've tried adding sleep's to allow for the time to read the text file.

Code: Select all

::]focus::
; Initialize an empty array
lines := []

; Open the text file for reading
FileRead, fileContent, focus.txt
Sleep 100
; Split the file content into lines
Loop, Parse, fileContent, `n, `r
{
    ; Append each line to the array
	
	if (A_LoopField != "") 
	{
		lines.push(A_LoopField)
    }
}

; Now you have each line stored in the 'lines' array
; You can access individual lines like lines[1], lines[2], etc.
Sleep 100
; Example: Output each line to the MsgBox
; I added the following to test and see if I was reading in the text
for index, line in lines
{
    MsgBox Line %index%: %line%
}
; No message box is displayed

; Get a random item from the array
Random, focus_random, 1, % lines.Length()

myTemp := Clipboard
Clipboard := lines[focus_random]
Sleep 200
SendInput ^v
Sleep 200

Clipboard := myTemp

Return
I have the same problem with other code.
That one if I do it in 2 steps, it works fine: 1. Read the file and store in array. 2. Access the array and choose the data I want.
I don't understand why I can't just do it all in one operation.

This line:

Code: Select all

MsgBox Line %index%: %line%
Doesn't get executed - can't understand why.

Thanks.
ShatterCoder
Posts: 88
Joined: 06 Oct 2016, 15:57

Re: Text shortcut to load file and read and then make shortcut - doesn't work

Post by ShatterCoder » 13 Mar 2024, 12:55

Your code appears to be fine. I suspect the issue is with reading the file.

Is "Focus.txt" in the same folder as your script?
ShatterCoder
Posts: 88
Joined: 06 Oct 2016, 15:57

Re: Text shortcut to load file and read and then make shortcut - doesn't work

Post by ShatterCoder » 13 Mar 2024, 13:12

I was able to verify that your code is ok by the simple expediant of replacing the fileread line with the following:

Code: Select all

;~ FileRead, fileContent, focus.txt
fileContent :=
(
"Line1 is this line
Line2 says something else
Line3 is that line"
)
So either the text file is not in the same folder as your script, or it's an empty file which is why the for loop never executes (the array has 0 items in it). It's the same as running `loop, 0`
gregster
Posts: 9114
Joined: 30 Sep 2013, 06:48

Re: Text shortcut to load file and read and then make shortcut - doesn't work

Post by gregster » 13 Mar 2024, 13:19

In doubt, you can also check Errorlevel and A_LastError for FileRead: https://www.autohotkey.com/docs/v1/lib/FileRead.htm#Error_Handling
Or simply try a msgbox % fileContent.
Post Reply

Return to “Ask for Help (v1)”