Jump to content


Photo

AHK to read a text file, copy first line/paste and loop


  • Please log in to reply
16 replies to this topic

#1 matt20687

matt20687
  • Members
  • 9 posts

Posted 14 May 2012 - 06:55 PM

Hello,

I am fairly new to coding AHK, i need to adapt the code i have below to read a .txt file called id.txt which consists of a bunch of ID's each on its own line. I need it to read the first line, copy it and paste it where i have stated below with the command send %clipboard%. All of the rest of the script below works fine but to be able to paste the information i need, i need to be able to loop around and look at the 1st line, then the 2nd and so forth.

ifwinexist, ahk_class Chrome_WidgetWin_0
{
	winactivate, 
	sleep, 750
	send ^fPatient ID
	sleep, 750
	Send {Escape}
	sleep, 750
	send {tab}{tab}{tab}{tab}
	send %clipboard%                               
	sleep, 750
	send {tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}
	send {enter} [color=#00BF00];Commit Edits[/color]
	sleep, 750
	send {Shift Down}
	send {tab}
	send {Shift Up}
	sleep, 750
	send {enter} [color=#00BF00];Confirm[/color]
	return
}
else	MsgBox,0, Google Chrome Is Not Open!
return

Thanks,
Matt

#2 Pulover

Pulover
  • Members
  • 1241 posts

Posted 14 May 2012 - 07:42 PM

You can use GoSub to execute your macro after each line:

SetWorkingDir %A_ScriptDir%

Loop, read, id.txt
{
	Clipboard = %A_LoopReadLine%
	GoSub, PasteInfo
}
return

PasteInfo:
ifwinexist, ahk_class Chrome_WidgetWin_0
{
winactivate, 
sleep, 750
send ^fPatient ID
sleep, 750
Send {Escape}
sleep, 750
send {tab 4}
send %clipboard% 
sleep, 750
send {tab 14}
send {enter} ;Commit Edits
sleep, 750
send {Shift Down}
send {tab}
send {Shift Up}
sleep, 750
send {enter} ;Confirm 

return

}
else MsgBox, Google Chrome Is Not Open!
return


Btw, {tab}{tab}{tab}{tab} is the same as {tab 4}, and also use [code=auto:0] tags in your next posts, please.

#3 0x150--ISO

0x150--ISO
  • Members
  • 657 posts

Posted 14 May 2012 - 07:43 PM

You could pre-read every line into a pseudo array
Loop, Read, id.txt [color=#008000]; File Read Loop[/color]

  IDArr%A_Index% := A_LoopReadLine

[color=#008000]; The above line places the current files line into the pseudo array IDArr

; You can then call the lines content by using the var with the line number you want.

; For instance IDArr1 will contain id.txt line 1 IDArr2 line 2 and so on...[/color]



[color=#008000]; Your code goes below

; .................[/color]



send %IDArr2%  [color=#008000]; Example of how to recall line 2[/color]
This is what your after?

#4 Blackholyman

Blackholyman
  • Members
  • 1200 posts

Posted 14 May 2012 - 07:44 PM

Trie this loop, read

Like this maybe
Loop, read, C:\Id.txt
    Id_line[%A_index%] := A_LoopReadLine
Clipboard := id_line[[color=#FF0000]#[/color]]
# is the number of the line you like to put in the clipboard :)

Hope it helps


#5 matt20687

matt20687
  • Members
  • 9 posts

Posted 21 May 2012 - 08:07 PM

Hello All,

I am developing a Batch Script which i will not go into too much detail and within the batch script i am calling on a couple of AHK scripts.

I am a little stuck and i was wondering whether anyone had experienced a similar thing, the bit i am stuck on does:

1.Searches a text file for a ID and sets it as a variable
2.Inputs the variable into a web addy and opens chrome with the full addy
3.Delays for 6 seconds
4.Runs a AHK script (converted to .exe)
5.Delays for 6 seconds
6.Loops back round to the start (1. above) and looks at the next ID in the text file. This carries on doing this until there are no IDs left in the text file to search

The section of this i am stuck on is the AHK part. The AHK script basically imitates tabs and returns and then selects a field from a drop down box on the webpage, this information is governed at the start of my batch script where you manually enter the field you need AHK to select, its a simply copy paste jobby. This is not good for me to be honest and i need to change it so AHK can look at a separate text file and work on a loop alongside the loop within my batch script.

The big question is (sorry for the rambling!), can you have a loop working in a batch script that will call on a AHK file that also has a loop within. At the moment i have struggled to get the two to work alongside each other, AHK either just looks at the first entry in the text file or trys to do the loop through the whole text file every time it is called (in some cases it may be 80 times!).

Does anyone have any ideas? By all means fire questions at me.

My code for the AHK script is (this was the start where it pasted the variable within the batch script):
ifwinexist, ahk_class Chrome_WidgetWin_0
                      {
                                      winactivate, 
                                      sleep, 750
                                      send ^fPatient ID
                                      sleep, 750
                                      Send {Escape}
                                      sleep, 750
                                      send {tab}{tab}{tab}{tab}
                                      send %clipboard%                               
                                      sleep, 750
                                      send {tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}{tab}
                                      send {enter} ;Commit Edits
                                      sleep, 750
                                      send {Shift Down}
                                      send {tab}
                                      send {Shift Up}
                                      sleep, 750
                                      send {enter} ;Confirm                                
                                      
                      return
                      
                      }
                      else MsgBox,0, Google Chrome Is Not Open!
                      return


#6 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 21 May 2012 - 08:22 PM

one thing to note is that Google Chrome v19 is Chrome_WidgetWin_1 not Chrome_WidgetWin_0.

would you consider using AHK to replace the batch part entirely? It may save some time in the long run.

other notes:
send {tab}{tab}{tab}{tab} easier to read: --> send {tab 4}

Loop, read, filename.txt is a good starting point for doing something based on each line in a file.

We'll need a little more example to give you better direction, I think.

chrome := "ahk_class Chrome_WidgetWin_0" ; or 1!
;1.Searches a text file for a ID and sets it as a variable
Loop, Read, TextFile.txt
{
InStr or RegexExMatch to find the ID in the A_LoopField -> myID and any other variables

;2.Inputs the variable into a web addy and opens chrome with the full addy
baseURL := "http://mywebapp.com/crazy/thing?id=MYID&blob=BLOB"
StringReplace, myURL, baseURL, MYID, %myID%
StringReplace, myURL, myURL, BLOB, %myBLOB%
Run, %A_ProgramFiles%\Google Chrome\chrome.exe %myURL%

;3.Delays for 6 seconds
Sleep, 6000
WinActivate, %chrome%

;4.Runs a AHK script (converted to .exe)
FileReadLine or Loop, Read a file for instructions

;5.Delays for 6 seconds
Sleep, 6000

;6.Loops back round to the start (1. above) and looks at the next ID in the text file. This carries on doing this until there are no IDs left in the text file to search
WinClose, %chrome%

}


#7 matt20687

matt20687
  • Members
  • 9 posts

Posted 21 May 2012 - 08:31 PM

Hello,

The batch script is fairly complex and to be honest i would prefer not to change it, at present it rips the html out of a web page (before it gets to the steps above), dumps it in a txt file, searches for three different strings which then outputs 3 different variables that are used in other parts of the script.

All i would like AHK to do is to do the key strokes as i have already shown in my code but instead of pasting the clipboard i would like to have it look at a txt file and work its way down each time the AHK script is called from the batch script. I was thinking if there was a way of AHK knowing which one it had just looked at and then going onto the next line and so on. Is this possible?

#8 engunneer

engunneer
  • Fellows
  • 9162 posts

Posted 21 May 2012 - 08:46 PM

if you can keep track of your line in the batch file, you can pass an argument to AHK telling it which line to read. (FileReadLine)

Alternately, you could have AHK use iniWrite and iniRead to keep track of data externally.

or you could have AHK work on just the first line of a file, then use hugoV's TF library to delete the line you worked on. when you come back around, just work on the new first line again.

#9 matt20687

matt20687
  • Members
  • 9 posts

Posted 22 May 2012 - 08:36 AM

Thanks for this enguneer.

I would prefer not to delete the text within the .txt file to be honest. I do not quite understand iniWrite and iniRead, are you or anyone else able to help me out?

Thanks,
Matt

#10 matt20687

matt20687
  • Members
  • 9 posts

Posted 22 May 2012 - 08:40 AM

Alternatively if it is easier i can use hugoV's TF library so i am always working on the first line.

I know i said it would not be preferable but in this instance it wouldnt be too bad, does anyone have a link to which one i need to download on hugoV's TF library.

#11 Guests

  • Guests

Posted 22 May 2012 - 08:46 AM

<!-- m -->http://www.autohotkey.net/~hugov/<!-- m -->

#12 matt20687

matt20687
  • Members
  • 9 posts

Posted 22 May 2012 - 08:48 AM

http://www.autohotkey.net/~hugov/


Thanks, which one of these do i need though?

#13 Guests

  • Guests

Posted 22 May 2012 - 08:55 AM

Not sure what you mean. Do you mean which TF functions? On the page linked above the first three deal with TF. The documentation, the library and the link to the forum thread to ask for help/suggestions.

I haven't read the thread above but it seems you want to read and remove lines? If so look at
<!-- m -->http://www.autohotke... ... _ReadLines<!-- m -->
<!-- m -->http://www.autohotke... ... emoveLines<!-- m -->
but be sure to read the introduction and instructions otherwise your next question will be "how do I use it" or "why does it create a file_copy.txt instead over updating file.txt" ;-)

#14 matt20687

matt20687
  • Members
  • 9 posts

Posted 22 May 2012 - 01:28 PM

Not sure what you mean. Do you mean which TF functions? On the page linked above the first three deal with TF. The documentation, the library and the link to the forum thread to ask for help/suggestions.

I haven't read the thread above but it seems you want to read and remove lines? If so look at
<!-- m -->http://www.autohotke... ... _ReadLines<!-- m -->
<!-- m -->http://www.autohotke... ... emoveLines<!-- m -->
but be sure to read the introduction and instructions otherwise your next question will be "how do I use it" or "why does it create a file_copy.txt instead over updating file.txt" ;-)


Hello,

The principal of ReadLines and RemoveLines is pretty simple, one thing i would like to stop is when you remove the line to not create a new file instead amend the .txt file. Is the only way to do this to delete the original and rename the copy?

#15 Guests

  • Guests

Posted 22 May 2012 - 01:33 PM

Like I said, read the instructions ;-)
You have to use the ! prefix, so TF_RemoveLines("!file.txt"

See "Quick intro to Parameters" and "Textfile and the ! Prefix." as described here <!-- m -->http://www.autohotke...ugov/tf-lib.htm<!-- m -->