Jump to content

Write to file each time window is opened


Recommended Posts

I am looking for some help with my code. I want to be able to have this script always run, and append a file each time a program is opened adding a new line. Here is my code, and help would be greatly appreciated.

#include <MsgBoxConstants.au3>
#include <FileConstants.au3>
#include <File.au3>

Local $aLines, $iCount = 0
local $hDate = @YEAR & '-' & @MON & '-' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC
local $hfile=FileOpen("C:\Users\public\Desktop\new1.txt", $FO_APPEND)
Local $hComputername = @ComputerName ; Computer Name
Local $hUsername = @UserName ; User Name


While 1

    If $hfile = -1 Then
        SplashTextOn("Error", "Unable to open and append the log file.", 300, 50)
        Sleep(2000)
        SplashOff()
    EndIf

        _FileReadToArray("C:\Users\public\Desktop\new1.txt", $aLines)
        If Not @error Then
            ; if the file exists look for the ID starting on the last line
            For $i = $aLines[0] To 1 Step -1
                If StringLeft($aLines[$i], 2) == "id" Then
            ; get the ID number
                    $iCount = Int(StringRegExpReplace($aLines[$i], "id(\d*):.*", "$1"))
                    ExitLoop
                EndIf
            Next
            ; Check we found an ID
            If $i = 0 Then
                MsgBox(0, "Error", "No line id found")
                Exit
            EndIf
        EndIf


    If WinExists("[Class:Notepad]", "") Then
            FileWrite($hfile, _LineID() & '|' & $hDate & '|' & $hComputername & '|' & "Notepad was opened by " & $hUsername & @CRLF)
            Sleep(10)
            FileClose($hfile)
            Sleep(1000)
    EndIf


WEnd


Func _LineID()
    $iCount += 1
    Return "id" & StringFormat("%04i", $iCount) & ": "
EndFunc ;==> LineID

Thanks!

Edited by grimmlock

Thanks

Grimm

Link to comment
Share on other sites

This a relatively simple approach to your question:

While 1
        If Not WinExists(WinGetHandle("Registry Editor")) Then
                ContinueLoop
        Else
                MsgBox(0, "", "Win now exists") ;write info to file...
        EndIf
        Sleep(50)
WEnd
Link to comment
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
 Share

×
×
  • Create New...