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 post
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 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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By poddex
      Hello everyone.
      I always try to work with old Windows 10 versions as much as possible because I know mane compatibility issues with Windows 10 upgrading. 
      But I couldn't do anything else (I got drivers problem), and nothing couldn't help me besides upgrading, so I upgraded from 1807 to 1903.
      And...get another problem 😃
      I use AutoitX library in external project like this.
      ObjectAutoIt=New COMObject("AutoItX3.Control");
      ObjectAutoIt.AutoItSetOption("WinTextMatchMode",2);
              While ObjectAutoit.WinExists("",WindowHeader) Cycle 
                  ObjectAutoIt.WinClose("",WindowHeader);
              EndCycle;
      After upgrade I get that this line code 
      While ObjectAutoit.WinExists("",WindowHeader)
      become extremely low - ~ 20 seconds even if 10 windows open. But before upgrade it takes 0.5 s for a max.
      And every time that this code line passes through  - it takes ~20s, (20.115, for example), not less, not more. Something pauses it to work.
      How can I diagnose, what is that?
      I tried reinstall whole AutoIt, but no results.
      Thanks to all.
    • By cabrunco
      Hi all, I am writing some scripts to control some lab equipment, and sometimes I observe that the script "misses" one of the steps, leading to cascading errors. For example, at one part of the code I wrote this:
      WinClose("Port_Configuration")
      Sleep(1000)
      WinClose("Hype")
      Sleep(1000)
      Run("C:\Users\cabrunco\Desktop\Hype!Terminal.exe")
      Most of the times this works, but I found that in some occasions the windows fail to close. Then, the code becomes a mess, because a second instance of the program will launch, and both windows now have the same title. I think I can solve this problem with this code:
      While WinExists("Hype")
          WinClose("Port_Configuration")
          Sleep(1000)
          WinClose("Hype")
          Sleep(1000)
      WEnd
      Run("C:\Users\matheus\Desktop\Hype!Terminal.exe")
      I would like to know if anybody has any other suggestions for this kind of redundant code. Thanks,
      Matheus
       
       
       
    • By jantograaf
      Hi all,
      I'm having some trouble restoring a program which uses a normal AutoIT-GUI and is used as a ToolKit for our customers to open different (external) tools, check their network connections or ping different destinations. Almost all customers are running this script on Win7 x64. 
      What do I want to do?
      When the program is already running and the user tries to start it for a second time, it should exit the second instance after activating/restoring the first instance.
      What did I do to integrate this?
      I used a little piece of script found on the forums to prevent opening a second instance and modified it as follows:
      $g_szVersion = "VSOL ToolKit" If WinExists($g_szVersion) Then Local $gState = WinGetState($g_szVersion) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_SHOW) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinSetState($g_szVersion,"",@SW_RESTORE) MsgBox(0,"","gState = " & $gState) ;For debugging purposes WinActivate($g_szVersion) Exit EndIf AutoItWinSetTitle($g_szVersion) I have tried it with only the @SW_SHOW, only the @SW_RESTORE, both of them in a different order, same difference, it doesn't work.
      What does happen then?
      It detects the current state (when it's minimized) as '5', which means enabled and hidden. After setting the state to show, the state changes to 7. Restoring doesn't change the state. But what is really odd, is that the window doesn't appear but AutoIt seems to create a néw window with the same title as the existing one. 

      It doesn't matter if the window was originally minimized by clicking the minimize button in the GUI or by a line of code in my script. For example, when users open a software package from within my script, a similar function to this one will be called:
      Func VSOL_FFTOOLS_SHOW_DOWNLOADS() GUISetState(@SW_MINIMIZE,$VSOL_BACKEND) ShellExecute(@UserProfileDir & "\Downloads") EndFunc The only thing that does work, is when the window is running in a non-minimized state (somewhere behind the current window), it gets activated and receives the focus when trying to start the second instance.
      If anyone could help me out with this one, I'd be forever thankful :-)
      Thanks in advance!
      Kind regards,
      Jan Geurts
    • By wimhek
      Is it possible , and how can I read and write txt files from Icloud (apple service) ?
      Let me try to explain my application.
      On my Ipad and Iphone I create txt files. On my windows computer it is possible to read and modify these files manually, by logging in on www.icloud.com.
      What I want to make is an auto-it script who reads the txt file and create an new txt file on www.icloud.com,  so I can acces these on my ipad and/or phone.
       
      Thank you.
    • By carl1905
      This code will read text data from txt file. However, if I use '$Newtext &= $convert' then $convert is saved as strings, not hex value.
      For example, if $convert = 0x300020FF and $bNewText = Test then the result is 0x333030303230464654657374. (Wrong)
      What I want is 0x300020FF54657374. How can I modify my script?
      For $i = $y-1 To Ubound($split) - 1 $convert = "0x" & $split[$i][0] $Newtext &= $convert $NewLen_hex = BinaryLen ($convert) $i=$i+1 $split[$i][1] = $split[$i][0] $split[$i][1] = StringRegExpReplace($split[$i][1], "<cf>", @CRLF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<lf>", @LF) $split[$i][1] = StringRegExpReplace($split[$i][1], "<cr>", @CR) $bNewText = _WinAPI_WideCharToMultiByte($split[$i][1], $CP_SHIFT_JIS) if $i = Ubound($split) - 1 Then $bNewText &= Chr(0) EndIf $Newtext &= $bNewText $NewLen = $NewLen_hex + BinaryLen ($bNewText) Next (...) (...) $Newfile = BinaryMid($Newfile_header,1) & BinaryMid($Newtext,1) $hNewfile = FileOpen ("NEW_"&$Name, 2+16) FileWrite ($hNewfile, $Newfile) FileClose ($hNewfile) TrayTip ("Importer", "Finish!", 3) sleep (3000)  
×
×
  • Create New...