Jump to content
Sign in to follow this  
mafioso

Need help with loop

Recommended Posts

mafioso

Hello. I am trying to make a logger for my PC. It logs Firefox's title and writes it to the line. But here's the problem : I've created an infinite loop, and script keeps writing to file for ex. hundreds and hundreds of lines while still on the same page. Here's the code, I hope you will understand what I want through code (read comments) :

Opt("WinTitleMatchMode", 2)

While 1
WinWait ("Mozilla Firefox")
$title = WinGetTitle ("Firefox")
$file = FileOpen ("logs.txt", 1)
FileWriteLine ($file, "Firefox opened at " & @MDAY & " (day in the month) " & @HOUR & " (hour) " & @MIN & " (minutes) " & "Firefox's title: " & $title & " {END OF LOG}" & " -----------")
$readline = FileReadLine ($file, 1)
$readline2 = FileReadLine ($file, 2)
---If $readline & readline2 are same, then pause the filewriting (while WinGetTitle is still working) UNTIL $title changes---
WEnd

So, if $readline & $readline2 are same, I need to pause the writing to the file (something like pausing the loop). WinGetTitle still needs to check, and when $title changes (look at filewriteline), continue loop. I hope you understood me...really hard to explain...When $title changes, then FileWriteLine will write different line (3rd line) than the 1st line...ah...

Share this post


Link to post
Share on other sites
FireLordZi

Hello. I am trying to make a logger for my PC. It logs Firefox's title and writes it to the line. But here's the problem : I've created an infinite loop, and script keeps writing to file for ex. hundreds and hundreds of lines while still on the same page. Here's the code, I hope you will understand what I want through code (read comments) :

Opt("WinTitleMatchMode", 2)

While 1
WinWait ("Mozilla Firefox")
$title = WinGetTitle ("Firefox")
$file = FileOpen ("logs.txt", 1)
FileWriteLine ($file, "Firefox opened at " & @MDAY & " (day in the month) " & @HOUR & " (hour) " & @MIN & " (minutes) " & "Firefox's title: " & $title & " {END OF LOG}" & " -----------")
$readline = FileReadLine ($file, 1)
$readline2 = FileReadLine ($file, 2)
---If $readline & readline2 are same, then pause the filewriting (while WinGetTitle is still working) UNTIL $title changes---
WEnd

So, if $readline & $readline2 are same, I need to pause the writing to the file (something like pausing the loop). WinGetTitle still needs to check, and when $title changes (look at filewriteline), continue loop. I hope you understood me...really hard to explain...When $title changes, then FileWriteLine will write different line (3rd line) than the 1st line...ah...

maybe lookup Do..Until

While Alive() {
	 DrinkWine();
}
AutoIt Programmer

Share this post


Link to post
Share on other sites
PsaltyDS

Just don't write anything in the first place if the title hasn't changed. Also, the title may not contain "Mozilla Firefox", so use the handle. Also, also, the _FileWriteLog() function will add a date/time stamp automatically. Also, also, also, you need a way to exit when the window closes:

#include <file.au3>

Opt("WinTitleMatchMode", 2)

Global $file = "C:\Temp\FFLog.txt"

WinWait("Mozilla Firefox")
Global $hFF = WinGetHandle("Mozilla Firefox")
Global $sTitle = WinGetTitle($hFF)
_FileWriteLog($file, "Firefox open, title = " & $sTitle)

While WinExists($hFF)
    $sNewTitle = WinGetTitle($hFF)
    If $sNewTitle <> $sTitle Then 
        $sTitle = $sNewTitle
        _FileWriteLog($file, "Firefox changed, title = " & $sTitle)
    EndIf
    Sleep(100)
WEnd

_FileWriteLog($file, "Firefox closed.")

:P

Edit: Sleep() added so ReaImDown doesn't fry any more RAM... :D

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
mafioso

Thanks a LOT !

Share this post


Link to post
Share on other sites
ReaImDown

Just don't write anything in the first place if the title hasn't changed. Also, the title may not contain "Mozilla Firefox", so use the handle. Also, also, the _FileWriteLog() function will add a date/time stamp automatically. Also, also, also, you need a way to exit when the window closes:

#include <file.au3>

Opt("WinTitleMatchMode", 2)

Global $file = "C:\Temp\FFLog.txt"

WinWait("Mozilla Firefox")
Global $hFF = WinGetHandle("Mozilla Firefox")
Global $sTitle = WinGetTitle($hFF)
_FileWriteLog($file, "Firefox open, title = " & $sTitle)

While WinExists($hFF)
    $sNewTitle = WinGetTitle($hFF)
    If $sNewTitle <> $sTitle Then 
        $sTitle = $sNewTitle
        _FileWriteLog($file, "Firefox changed, title = " & $sTitle)
    EndIf
WEnd

_FileWriteLog($file, "Firefox closed.")

:party:

u missed some sleeps...it will use 99% of the cpu :) (I learned that the hard way, I fried some ram) :)

[u][font="Century Gothic"]~я α и d γ ĵ . ċ . ѕ қ ϊ и и ε я~- My Programs -auto shutdownSleep funcdisallow programs[/font][/u]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×