AutoHotkey Community

It is currently May 27th, 2012, 11:38 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: August 26th, 2005, 12:01 am 
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


Report this post
Top
  
Reply with quote  
 Post subject: getting an error
PostPosted: February 3rd, 2009, 2:00 pm 
line 37? something about time=+1


Report this post
Top
  
Reply with quote  
 Post subject: Re: getting an error
PostPosted: February 3rd, 2009, 5:11 pm 
Offline

Joined: October 13th, 2008, 4:14 pm
Posts: 60
Location: South Park, Colorado
companycmd wrote:
line 37? something about time=+1

At line 25, add the code that follows.
Code:
StringReplace, title, title, \,,All


edit: That takes the back slashes out of paths. If you need them back in for readability, replace them later.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: nomissenrojb and 62 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group