Help with searching/analyzing a text file Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ThomasPaine
Posts: 25
Joined: 24 Jun 2017, 22:17

Help with searching/analyzing a text file

24 Jun 2017, 22:19

Hello! I need some help to analyze a text file that gets modified every 5 minutes or so. If the file gets modified, I want to check 4 specific lines in the text file. If the conditions are met, I want it to send a mouseclick at specific coordinates. For the most part, I have a pretty good idea of the structure of the script (at least I think so). I tried to write it (even though it's obviously incorrect because I'm blatantly typing in English instead of AHK on the parts I don't know).

Code: Select all

~^x::
{
    loop
    {
        if practice.txt gets modified then do
        {
            ; check line 24
            if Line24Doesn'tContainText ("FFE4C4" or "808080" or "00BFFF" or "0B0000")
            then
            {
                click 150,150
            }
            return
            
            sleep 50
            
            ; check line 33
            if Line33Doesn'tContainText ("FFE4C4" or "808080" or "00BFFF" or "0B0000")
            then
            {
                click 150,200
            }
            return
            
            sleep 50
            
            ; check line 42
            if Line42Doesn'tContainText ("FFE4C4" or "808080" or "00BFFF" or "0B0000")
            then
            {
                click 150,250
            }
            return
            
            sleep 50
            
            ; check line 51
            if Line51Doesn'tContainText ("FFE4C4" or "808080" or "00BFFF" or "0B0000")
            then
            {
                click 150,300
            }
            return
        }
        return
        
        sleep 250
    }
    return
}
return
This is the text file that I will be scanning (practice.txt) with 67 lines in total with the last two being empty:

Code: Select all

<html><head><title>AHK Practice</title><body>
Hello Buddy
<br1>
Pew1 <font color="808080">Pew2</font>Pew3
<center>
<img src="UI.SquareWhite" width=270 height=1><img src="UI.SquareBlank" width=1 height=3>
<br1>
<table width=270 border=0 bgcolor="000000">
<tr>
</tr>
<tr>
</tr>
<tr>
<td fixwidth=20 align=left></td>
<td fixwidth=135 align=left><1><font color="FF0000">Column 1</font></td>
<td fixwidth=115 align=left><2><font color="FF0000">Column 2</font></td>
<tr>
</tr>
<tr>
</tr>
<tr>
<td fixwidth=20 align=left></td>
<td fixwidth=135 align=left><1><font color="808080">button</font></td>
<td fixwidth=115 align=left><2><font color="818082">button</font></a></td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td fixwidth=20 align=left></td>
<td fixwidth=135 align=left><1><font color="808080">button</font></td>
<td fixwidth=115 align=left><2><font color="FFE4C4">button</font></a></td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td fixwidth=20 align=left></td>
<td fixwidth=135 align=left><1><font color="808080">button</font></td>
<td fixwidth=115 align=left><2><font color="8B0000">button</font></a></td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td fixwidth=20 align=left></td>
<td fixwidth=135 align=left><1><font color="808080">button</font></td>
<td fixwidth=115 align=left><2><font color="00BFFF">button</font></a></td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
<img src="UI.SquareWhite" width=270 height=1><img src="UI.SquareBlank" width=1 height=3>
<br>
<font color="FF0000">The bitter end.</font>
</body></html>
All lines containing a Hex-Color text/string will get modified, but I'm only interested in the lines 24, 33,42 and 51. One of those 4 lines will always meet the condition of not having the text "FFE4C4" or "808080" or "00BFFF" or "0B0000" and the line that does not have any of those strings should trigger a mouseclick. Once this is done, the script should sit still until the text file is modified again (a loop checking for the modification of practice.txt). Please help me out if you don't mind.
ThomasPaine
Posts: 25
Joined: 24 Jun 2017, 22:17

Re: Help with searching/analyzing a text file

24 Jun 2017, 22:44

I'm guessing I need something like this, but even when I try to watch the help file or find examples, I'm not too sure how to use it.

Code: Select all

FileReadLine, 23, C:\My Documents\practice.txt, %A_Index%
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with searching/analyzing a text file

25 Jun 2017, 07:22

Hi, here's an example with two lines and two colors. I commented it. Here's some refrences (documentation):
. return
. SetTimer
. Arrays
. Associative arrays
. InStr function


Code: Select all

html := % A_MyDocuments . "\practice.txt"
lines := [ 24, 33 ]
; ========= example (uncomment to see intuitively how it works) =========
; Loop % lines.length()
	; MsgBox % a_index . " = " . lines[a_index] ; the dot is used to concatenate strings
	
clicks := {24:[ 150,150 ], 33:[ 150,200 ]} ; an associative array where each key (line) is associated with one array (of two coordinates)
; ========= example (uncomment to see intuitively how it works) =========
; for line, array in clicks  ; loop throught each key(line)-value(array) pair in the lines object
	; Loop % array.length()
		; MsgBox % "line:" . line . A_Space . " | element " . a_index . " = " . array[a_index]

#Persistent
SetTimer, check, 1000 ; define a timer; the subroutine will be launched automatically and repeatedly at a specified time interval here 1 minute (60000ms); reduce it for tests.
return ; end of the auto-execute section

check: ; 'check' subroutine
Loop % lines.length()
{
currentLine := lines[a_index]

	FileReadLine, lineContent, % html, % currentLine ; read the current loop iteration line
	
		if not (InStr(lineContent, "FFE4C4") and InStr(lineContent, "808080")) ; InStr can search  for a given occurrence of a string
		{
		x := clicks[currentLine][1], y := clicks[currentLine][2] ; retrieves and store in x/y the first/second element of the array of coordinates
			Click %x%, %y%
		}
		
}
return ; end of the 'check' subroutine
Hope it helps; good luck!
my scripts
ThomasPaine
Posts: 25
Joined: 24 Jun 2017, 22:17

Re: Help with searching/analyzing a text file

25 Jun 2017, 12:15

Thanks a lot, you're awesome. I'm quite busy today but as soon as I have a moment to spare I'll take a look at it and ask questions later :)

Edit: Hey, I checked out the code and understood most of it. I tested with MsgBox %CurrentLine% to see if it works well and it seems to work flawlessly except for the loop being an arbitrary time frame rather then only checking when the file is modified. If the file is modified, the click has to happen right away with only a margin of 2-5 seconds of delay at most. If I leave it as it is but with a small delay, it will spam clicks. If I put a long delay in the loop, it will click too late. I was looking in the help documents and also searched a bit on the forums and I think this is the right method to do it? https://autohotkey.com/docs/commands/FileGetTime.htm What do you think, and how can I implement it? Perhaps it's not even when the file is modified but rather when it's accessed. I'm not sure if modifying the file with the exact same text would count as a modification. For example, if a text file contains "1" and then gets modified to "1" or "2" or "3" or "4" does that count as a modification if it ends up being from "1" to "1"? Perhaps FileGetTime, ModifiedTime [, C:\My Documents\practice.txt, A] Then do something like PreviousModifiedTime := !ModifiedTime then do the loop to check the lines. When the line that meets the condition is found, I could add something like ModifiedTime := PreviousModifiedTime. Maybe I'm wrong, I'm just putting my thoughts out there because I feel bad if I made no effort and just asked you to hand me the answer. I will still try to do it on my own with the help file or if I find any good examples, but if it's not much trouble, please help me out once more. I do understand your code (the annotations are very helpful too!). Thanks!
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
Location: France
Contact:

Re: Help with searching/analyzing a text file  Topic is solved

25 Jun 2017, 17:41

Hi,

Sorry just noticed your edit.
ThomasPaine wrote:If the file is modified, the click has to happen right away with only a margin of 2-5 seconds of delay at most.
Ok ; however, you can avoid useless loops ; first checking for any change at fast interval in an inner loop (for instance one second) and once the file has actually been modified break this inner loop and enter in the outer loop that will execute again the inner loop at long interval (~4minutes since the text file is modified every 5 minutes or so). As you pointed out, here you can take advantage of the FileGetTime command, as you described.

Code: Select all

htmlFile := % A_MyDocuments . "\practice.txt"
lines := [ 24, 33 ]
FileGetTime, t1, % htmlFile, M ; get the start reference datetime stamp of the file 

Gosub, checkForChange
return ; end of the auto-execute section

checkForChange: ; 'checkForChange' subroutine
Loop { ; outer loop

	Loop { ; inner loop
	
		FileGetTime, t2, % htmlFile, M
		ToolTip, checking for changes...
		if not (t2 == t1) ; if the last time the file has been modified isn't the same as the previous time
		break ;  terminates the loop
		; otherwise...
		sleep, 1000 ; check for each second
	}
		; ...otherwise
		ToolTip ; remove tooltip
		t1 := t2 ; reinitialize t1
		Loop % lines.length()
		{
		currentLine := lines[a_index]

			FileReadLine, lineContent, % htmlFile, % currentLine ; read the current loop iteration line
			MsgBox % lineContent
			
			; ... rest of the code

		}
		sleep -240000 ; 240000ms (4 minutes?) to avoid useless checking since the text file is modified every 5 minutes or so
	
}
return ; end of the 'check' subroutine


!x::ExitApp ; a hotkey to exit app ALT+X
my scripts

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Nerafius, RandomBoy and 184 guests