Jump to content

Create log of active window


Recommended Posts

Is there an way to create a log file every day wich contains the active screens of a specified program or process or PID?

What i want to create is a log wich contains the minutes how long a Remote Desktop windows is opened and active.

I could create a while loop for a process excist then write a line to a text file with the PID and the name of the process or application, but i don't know how to start to check the time of an active window.

To create some kind of a counter wich can count the minutes.

Can anyone help me trying to create such kind a thing or at least push me in the right direction?

Link to comment
Share on other sites

I have created this script:

While 1
Opt("WinTitleMatchMode", 2)

$state = WinGetState("Notepad", "")

If $state = "Notepad" Then
    
Else
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    $Window = WinGetTitle("")
    WinWaitNotActive("Notepad")
    $dif = TimerDiff($begin)
    $EndTijd = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringTrimRight($WorkedTime, 14)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd

For testing purpose i used Notepad and it works fine. But if i change the Title in Remote Desktop, i get multiple lines of some windows i never had touch.

So i think this code i made is a mess of all different kinds error, but i don't know where. Can someone help me clarify this a bit?

Link to comment
Share on other sites

I have created this script:

While 1
Opt("WinTitleMatchMode", 2)

$state = WinGetState("Notepad", "")

If $state = "Notepad" Then
    
Else
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    $Window = WinGetTitle("")
    WinWaitNotActive("Notepad")
    $dif = TimerDiff($begin)
    $EndTijd = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringTrimRight($WorkedTime, 14)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd

For testing purpose i used Notepad and it works fine. But if i change the Title in Remote Desktop, i get multiple lines of some windows i never had touch.

So i think this code i made is a mess of all different kinds error, but i don't know where. Can someone help me clarify this a bit?

You're testing the return from WinGetState() wrong: it doesn't evaluate to a string.

You want something more like this:

Opt("WinTitleMatchMode", 2)
$title = "Notepad"
While 1
 
If Not WinExists($title) Then ;if the app window doesn't exist then just idle around
    Sleep(100)
    ContinueLoop
EndIf

$state = WinGetState($title) ;once app window exists, get its state
If BitAnd($state,8) Then ;8 indicates the specified Window is active
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    WinWaitNotActive($title)
    $Window = WinGetTitle("") ;moved this after the app becomes inactive
    $dif = TimerDiff($begin)
    $EndTime = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringFormat("%.2f",$WorkedTime)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd

Also changed the StringTrimRight() in your $result to a StirngFormat()

Link to comment
Share on other sites

You're testing the return from WinGetState() wrong: it doesn't evaluate to a string.

You want something more like this:

Opt("WinTitleMatchMode", 2)
$title = "Notepad"
While 1
 
If Not WinExists($title) Then ;if the app window doesn't exist then just idle around
    Sleep(100)
    ContinueLoop
EndIf

$state = WinGetState($title) ;once app window exists, get its state
If BitAnd($state,8) Then ;8 indicates the specified Window is active
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    WinWaitNotActive($title)
    $Window = WinGetTitle("") ;moved this after the app becomes inactive
    $dif = TimerDiff($begin)
    $EndTime = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringFormat("%.2f",$WorkedTime)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd

Also changed the StringTrimRight() in your $result to a StirngFormat()

This looks indeed better than my code.

The only thing i encoutered with my code and with this sample is the Test.txt is filled with some garbage instead of the title of the specific window.

12:16;12:16;Verbinding met Windows-sessie verbreken;0.03
12:16;12:16;Lokaal station (C:);0.04

Instead of this it should be something like this

12:16;12:16;123.123.123.123 - Remote Desktop;0.03

Where 123.123.123.123 is the IP adres of the remote controlled pc.

Any idea why this is going wrong? i Tried alot of things but still the garbage is staying.

Link to comment
Share on other sites

This looks indeed better than my code.

The only thing i encoutered with my code and with this sample is the Test.txt is filled with some garbage instead of the title of the specific window.

12:16;12:16;Verbinding met Windows-sessie verbreken;0.03
12:16;12:16;Lokaal station (C:);0.04

Instead of this it should be something like this

12:16;12:16;123.123.123.123 - Remote Desktop;0.03

Where 123.123.123.123 is the IP adres of the remote controlled pc.

Any idea why this is going wrong? i Tried alot of things but still the garbage is staying.

I knew I should have asked why you were doing a WinGetTitle("") before the target window became inactive!

If you look at the "garbage" you should be able to figure out what my version is doing:

it's capturing the title of the window the user switched to (because I assumed that you wanted to log what took them away from the target app).

So, when the target window is activated, that's when you want to do a WinGetTitle()

Opt("WinTitleMatchMode", 2)
$title = "Notepad"
While 1
 
If Not WinExists($title) Then ;if the app window doesn't exist then just idle around
    Sleep(100)
    ContinueLoop
EndIf

$state = WinGetState($title) ;once app window exists, get its state

If BitAnd($state,8) Then ;8 indicates the specified Window is active
    $Window = WinGetTitle($title) ;moved this immediately after the app becomes active to capture the full tiltle for logging 
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    WinWaitNotActive($title)
   
    $dif = TimerDiff($begin)
    $EndTime = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringFormat("%.2f",$WorkedTime)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd
Link to comment
Share on other sites

I knew I should have asked why you were doing a WinGetTitle("") before the target window became inactive!

If you look at the "garbage" you should be able to figure out what my version is doing:

it's capturing the title of the window the user switched to (because I assumed that you wanted to log what took them away from the target app).

So, when the target window is activated, that's when you want to do a WinGetTitle()

Opt("WinTitleMatchMode", 2)
$title = "Notepad"
While 1
 
If Not WinExists($title) Then ;if the app window doesn't exist then just idle around
    Sleep(100)
    ContinueLoop
EndIf

$state = WinGetState($title) ;once app window exists, get its state

If BitAnd($state,8) Then ;8 indicates the specified Window is active
    $Window = WinGetTitle($title) ;moved this immediately after the app becomes active to capture the full tiltle for logging 
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    WinWaitNotActive($title)
   
    $dif = TimerDiff($begin)
    $EndTime = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringFormat("%.2f",$WorkedTime)
    FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
EndIf

WEnd
You are my hero, this is what i get now if i run your script.

19:22;19:27;<IP ADRESS> - Remote Desktop;5.91
19:27;19:27;<IP ADRESS> - Remote Desktop;0.01

The funniest thing is the last line. Because when you click the X on the top of the window another screen appears with is warning you about that the programs you left open will be openen even after you closed the window etc.

I need to press Ok and thats it. But for that millisecond of a time is he creating a new line as shown above.

What is the best way to solve this? It is a minor issue and it would be perfect if even that was gone.

I have tried to put in another IF, a sleep command a For loop and more things just to explore the commands a bit, but on the way nothing appeared to be working for it.

Edited by Iznogoud
Link to comment
Share on other sites

You are my hero, ...

:)

... this is what i get now if i run your script.

19:22;19:27;<IP ADRESS> - Remote Desktop;5.91
19:27;19:27;<IP ADRESS> - Remote Desktop;0.01

The funniest thing is the last line. Because when you click the X on the top of the window another screen appears with is warning you about that the programs you left open will be openen even after you closed the window etc.

I need to press Ok and thats it. But for that millisecond of a time is he creating a new line as shown above.

What is the best way to solve this? It is a minor issue and it would be perfect if even that was gone.

I have tried to put in another IF, a sleep command a For loop and more things just to explore the commands a bit, but on the way nothing appeared to be working for it.

The reason for that is the "Disconnect Windows session" warning box that pops up when you click the X returns the focus to the Remote Desktop window after you hit the OK button. The only way around that I can think of would be to test if that window shows up and then sleep for a bit before resuming the script when that window disappears. That or don't write to the log file if the duration of activation is less than .01 or .02. That's probably the better/more reliable idea.

So:

Opt("WinTitleMatchMode", 2)
$title = "Remote Desktop"
$timeThreshold = .02 ;added this to give us something to check against for low periods of activity

While 1
 
If Not WinExists($title) Then ;if the app window doesn't exist then just idle around
    Sleep(100)
    ContinueLoop
EndIf

$state = WinGetState($title) ;once app window exists, get its state

If BitAnd($state,8) Then ;8 indicates the specified Window is active
    $Window = WinGetTitle($title) ;moved this immediately after the app becomes active to capture the full title for logging
    $Begin = TimerInit()
    $BeginTime = @HOUR & ":" & @MIN
    WinWaitNotActive($title)
    $dif = TimerDiff($begin)
    $EndTime = @HOUR & ":" & @MIN
    $WorkedTime = $dif/60000
    $result = StringFormat("%.2f",$WorkedTime)
    If Number($result) > $timeThreshold then ; if target window active only for a short time (like from "X"ing session), don't log the time
        FileWriteLine("C:\Test.txt", $BeginTime & ";" & $EndTime & ";" & $Window & ";" & $result)
    EndIf
EndIf

WEnd

So I added the new variable $timeThreshold that we now check against to see if the target app was inactive for longer than our threshold.

You may have to increase it if the systems your using take a little longer to close the window.

But I never had any that took more than .01 I just set it at .02 for a margin of safety.

Edited by ResNullius
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...