I was curious how much time I was spending doing various things throughout the day, so I hacked this up. It shows you how much time you've spent in each window, sorted descending. It's probably pretty nasty for anyone who has their head fully around AHK's syntax, so I'm *very* open to suggestions about how to improve readability, performance, functionality, etc
Code:
SetTimer, WindowTally, 1000
return
; This subroutine assumes you don't switch windows more often than the sub is called
WindowTally:
WinGetTitle, title, A
; We're going to use the window name as part of a pseudo-hash,
; so we need to remove any characters that aren't acceptable in
; variable names
StringReplace, title, title, -,,All
StringReplace, title, title, ',,All
StringReplace, title, title, .,,All
StringReplace, title, title, %A_Space%,,All
StringReplace, title, title, (,,All
StringReplace, title, title, ),,All
StringReplace, title, title, /,,All
StringReplace, title, title, :,,All
StringReplace, title, title, @,,All
StringReplace, title, title, <,,All
StringReplace, title, title, >,,All
StringReplace, title, title, [,,All
StringReplace, title, title, ],,All
StringReplace, title, title, ~,,All
; Consolidate a few windows
IfInString, title, Firefox
title = Firefox
IfInString, title, TOAD
title = Toad
IfInString, title, Statementprocessing
title = Toad
; Bump up the count for the currently focused window
IfNotInString, windowNames, %title%
windowNames := windowNames . title . ","
windowTime%title%++
return
#t::
; "output" will contain a line for each window
output =
; For each window, add a line to "output" with the amount of time
; spent in that window. Right now, this assumes a sample frequency
; of 1Hz
StringTrimRight, p, windowNames, 1
Loop, Parse, p, `,
{
totalTime := windowTime%A_LoopField%
seconds := Mod(windowTime%A_LoopField%, 60)
minutes := Mod(Floor(windowTime%A_LoopField% / 60), 3600)
hours := Floor(windowTime%A_LoopField% / 3600)
timeDesc =
; Parse the number of seconds into a human-readable string
if hours > 0
{
if hours = 1
units = hour
else
units = hours
timeDesc = %hours% %units%
if minutes <> 0 || seconds <> 0
timeDesc := timeDesc . ", "
}
if minutes > 0
{
if minutes = 1
units = minute
else
units = minutes
timeDesc = %timeDesc%%minutes% %units%
if seconds <> 0
timeDesc := timeDesc . ", "
}
if seconds = 1
units = second
else
units = seconds
if seconds <> 0
timeDesc = %timeDesc%%seconds% %units%
; Create the line for the window, prepending the total number of seconds for later sorting
output := output . "(" . totalTime . ") " . A_LoopField . ": " . timeDesc . "`n"
}
; Sort the lines (descending) by total time spent in each window
Sort, output, NP2R
; Finally, take the total seconds in parens back out of the output
StringTrimRight, output, output, 1
cleanOutput =
Loop, Parse, output, `n
{
StringTrimLeft, cleanLine, A_LoopField, InStr(A_LoopField, ")") + 1
cleanOutput = %cleanOutput%%cleanLine%`n
}
MsgBox, %cleanOutput%
return