Page 1 of 1

Choose csv file to read from and send input to an opened app

Posted: 21 Apr 2024, 12:46
by filipemb
Hello!

I need help to understand what functions I should call in this script I am trying to write.

My idea is 1) the user should type the name of the csv file he wants to load; 2) the app moves the mouse to click on a specific button on the screen; 3) then it send to the app the csv content line by line

Eg.: teste.csv has a, g, d, arg, c, s, 22 in the first line.

From a to s, the content can be send as text-only or raw. However, the "22" (sixth row content) has to be translated into a specific number of left or right arrow keys touches, depending on the delta to 20.

Eg: if sixth row content is 22, then it should mean 2 right arrow key touches. If the sixth row content is 18, then it should mean 2 left arrow key touches.

After reading the Loop (Parse - https://www.autohotkey.com/docs/v1/lib/LoopParse.htm) page and specially this topic from AHW v.1 (https://www.autohotkey.com/board/topic/5880-read-csv-file-parse-send-pause/), I managed to create my script. But I am stuck on getting only the first row and line content and I am unable to move thru the other rows in the first line.

I have to say that a simple tab key press move thru the fields I need to fill out in the app I am using.

My current script is:

Code: Select all

WinWait, Editor Brasfoot 2020
WinWaitActive, Editor Brasfoot 2020
MouseClick, left, 701, 685
Loop, Read, C:\Users\Felipe Barbosa\Desktop\teste.csv
 Loop, parse, A_LoopReadLine, CSV
   {
       WinActivate, Editor Brasfoot 2000,
	SetKeyDelay -1, 0
	Send, {CAPSLOCK}%A_LoopField%{TAB}
	Sleep, 500
   }
[Mod edit: Moved topic from AHK v2 help since this is v1 code while it doesn't seem to be a translation request.]

Re: Choose csv file to read from and send input to an opened app  Topic is solved

Posted: 21 Apr 2024, 13:44
by mikeyww
Welcome to this AutoHotkey forum!
  1. The iteration number of the inner loop is given by A_Index.
  2. What is a row?
  3. What are the first five rows in your example?
  4. Why not subtract the value from 20 when the iteration number is six?
  5. What does your script actually do when you run it?
If you are new to AHK, I recommend using its current version, which is v2, instead of this older deprecated version that is no longer developed.

Re: Choose csv file to read from and send input to an opened app

Posted: 21 Apr 2024, 16:34
by filipemb
Hello!

2. By row I mean y position of the info eg: csv line 1, row 1, then comma csv line 1, row 2.

3. Example: a, g, d, arg, c, s, 22
1st row - a; 2nd row - g; 3rd row - d; 4th row - arg; 5th row - c; 6th - s; 7th row - 22

4. It seemed to me that the calculation of the delta would be the simplest way to get the right number of left/right keystrokes as I need to move the number selector to the left or right to represent the data of the 7th row in CSV.

5. as of now, when the .exe is open, it clicks on a button located on 701, 685 position in 1600x1050 screen.

I need it to get thru all the “rows” and lines of the csv file chosen.

Re: Choose csv file to read from and send input to an opened app

Posted: 21 Apr 2024, 16:53
by mikeyww

Code: Select all

#Requires AutoHotkey v1.1.33.11
Loop Read, teste.csv
 Loop Parse, A_LoopReadLine, CSV
 { Switch A_Index {
    Case 7:
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
   Sleep 500
 }
My opinion is that, within one row, you are looking at columns or cells but not rows.

You initially referred to a sixth row, and later to a seventh row.

Re: Choose csv file to read from and send input to an opened app

Posted: 23 Apr 2024, 13:06
by filipemb
Thank you very much!

You're right and I am sorry - I misunderstood row and column.

Your code got me where I needed. I have to add that for some reason I do not know I needed to add another column of data before the first data column so the app could got the right.

However, I'm unable to get the app to input the second line/row of data in my csv file.

Ex:
teste.csv content is -
1st line/row - 1,felipe,a,d,chil,arm,dri,18
2nd line/row - 2,sarah,g,e,bras,col,def,22

Re: Choose csv file to read from and send input to an opened app

Posted: 23 Apr 2024, 13:36
by mikeyww
I had no trouble with it. Test it in Notepad so that you can see what is being sent.

Re: Choose csv file to read from and send input to an opened app

Posted: 23 Apr 2024, 15:22
by filipemb
I tested it and the right information is being sent as you said.

I need it to 1) mouseclick the x,y position (underlined, already done), parse each string in the csv (already done, also underline). However, for each line, I need it to send "enter" key (also done) and then repeat the mouseclick for the next line.

I believe I should add a Loop function until break for those SendInput {Enter} and mouseclick functions, right? Is it enough to call these function each CSV line at a time?

Code: Select all

WinWait, Editor Brasfoot 2020
WinWaitActive, Editor Brasfoot 2020
[u]MouseClick, left, 701, 685[/u]
Loop, Read, C:\Users\Felipe Barbosa\Desktop\teste.csv
[u]Loop, parse, A_LoopReadLine, CSV[/u]
	 { Switch A_Index {
    Case 8:
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
  	Default: Send % "{Text}" A_LoopField "`t"
}
}
Sleep 500
[u]SendInput {Enter}[/u]

Re: Choose csv file to read from and send input to an opened app

Posted: 23 Apr 2024, 16:30
by mikeyww
Statements to be repeated in your loop belong inside the loop. It could be something like the following; may need adjustments.

Code: Select all

#Requires AutoHotkey v1.1.33.11
WinWaitActive Editor Brasfoot 2020
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ Click 701 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
   Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
  Sleep 500
  Send {Enter}
}

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 06:36
by filipemb
Hello.

When I add 2x the Loop, parse, A_LoopReadLine, CSV, I manage to get the app to load the 2nd line after my first "SendInput {Enter}" and my 2nd mouseclick on the x, y position chosen. However, the app sends the 1st row one more time and then go to the 2nd line info.

What am I doing wrong? Is it a matter of find the right Sleep delay time or code place?

Current code:

Code: Select all

WinWait Editor Brasfoot 2020
WinWaitActive Editor Brasfoot 2020
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ MouseClick, left, 701, 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
   Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
   }
  Sleep 200
   Send {Enter}
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ MouseClick, left, 701, 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
  Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
   Sleep 1000
   Send {Enter}
    }

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 06:58
by mikeyww
My first question is whether the script that I posted works. Test in Notepad to see the output.

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 07:07
by filipemb
Yes, it does work. However, it doesn't stop the input between the first line and the second line after the first line is fully sent. That may be the cause why I'm not getting one input at one time in the app I am using in my code. I need it to load and parse the csv file but send one row/line content by one.

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 07:14
by mikeyww
Let's fix the script before moving to add more code. You stated what the script does not do, but how should it achieve your goal? What event or action will indicate that the second line of the text file should be processed?

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 08:00
by filipemb
The event should be the send enter key input.

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 09:22
by mikeyww
The current script waits 500 ms, sends Enter, and then moves to the next line. You can verify that in Notepad or KeyHistory. What part of this should the script do differently?

To clarify: the current script sends the Enter key.

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 15:13
by filipemb
It should wait after the enter key and then loop (1) click on the x, y position; (2) send the 2nd line/row of data; (3) send enter key again and repeat until the end of the csv file.

With your code, I am only getting the 1st line of data sent. When I repeat the loop, read and loop, parse, I get through the other lines and data sent to the app. I get the 2nd line/row and the hypotetical 3rd and 4th line/row all right. The only error is getting 1st line/row twice and one of them wrongly. I needed to change the sleep/wait time. Does it have change whenever I change the amount of data sent?

I attached to this message my app's printscreen. There you can see all the right data input. I am trying to use AHK to automatize the data input in this app. As you can see, for each csv column (except the 1st one), there is a field that I need to fill up with the CSV info.

My current CSV:

Code: Select all

1,felipe barbosa,a,d,chil,arm,dri,18
2,sarah vieira,g,e,bras,col,def,22
3,chico lico,z,d,argen,des,mar,25
4,pacolao,l,d,holan,mar,pass,34
My current AHK code:

Code: Select all

WinWait Editor Brasfoot 2020
WinWaitActive Editor Brasfoot 2020
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ MouseClick, left, 701, 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
   Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
   }
  Sleep 400
   Send {Enter}
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ MouseClick, left, 701, 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
  Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
   }
   Sleep 1000
   Send {Enter}
    }
image.png
(32.73 KiB) Downloaded 42 times
image.png
(15.7 KiB) Downloaded 42 times

Re: Choose csv file to read from and send input to an opened app

Posted: 24 Apr 2024, 16:24
by mikeyww
Test in Notepad first, not in your other program. This helps you understand what the script does. Modify the script that I posted, but do not add a new loop.

The script follows the code exactly. If you need a new Sleep in your loop, etc., add it.

Re: Choose csv file to read from and send input to an opened app

Posted: 25 Apr 2024, 07:44
by filipemb
I added setkeydelay after your code snippet and it now works perfectly both in Notepad and in my app. I got the data input in the way I neded.

Tonight or tomorrow I will make a GUI to let the user chose which csv file to load and then hold the csv file chosen as variable. After that, I share the code here and it may help someone else in the forums.

Current code:

Code: Select all

WinWait Editor Brasfoot 2020
WinWaitActive Editor Brasfoot 2020
Loop Read, teste.csv        ; Loop through the lines of the CSV file
{ MouseClick, left, 701, 685             ; Relative to active window
  Loop Parse, A_LoopReadLine, CSV
   Switch COL := A_Index {  ; Column number
    Case 8:                 ; A number
     n := A_LoopField - 20
     Send % "{" (n < 0  ? "Left" : "Right") " " Abs(n) "}"
    Default: Send % "{Text}" A_LoopField "`t"
SetKeyDelay 200
  }
  Send {Enter}
   }
   Sleep 500