 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
lazygeek
Joined: 08 Feb 2009 Posts: 16
|
Posted: Wed Feb 25, 2009 5:20 pm Post subject: tiny computer usage tracker |
|
|
I'm spending way too much time on the computer.
Up to now, I was using a freeware app called TimeTracker to keep track of the total time I spend working on the computer per day. However it has a few nasty bugs and doesn't keep an accurate count under some circumstances. As it seems to be no longer developed, I tried looking for other free lightweight alternatives but couldn't find any, so I decided to quickly put something together using ahk.
Here you go. It's extremely simple (I'm new to the world of AHK) but i hope it can still be useful to some.
How to use it: there is nothing to configure, just launch it (best to have it
run at logon) and it will start tracking active usage time, based on mouse and keyboard input (the A_TIMEIDLE variable). Just hold down the right ctrl key to view the current usage for the day. The accuracy is +/-3 minutes.
For past data, you can check the track.txt file.
| Code: |
; TinyTracker 1.02 - A tiny computer usage tracker by lazygeek
;
; How to use it: there is nothing to configure, just launch it (best to have it
; run at logon) and it will start tracking active usage time, based on mouse
; and keyboard input (the A_TIMEIDLE variable). Just hold down the right
; ctrl key to view the current usage for the day. The accuracy is +/-3
; minutes.
menu, tray, icon, %A_WinDir%\system32\shell32.dll, 44
menu, tray, NoStandard
menu, tray, add, Exit, closeall
menu, tray, tip, TinyTracker 1.02
formatted :=0
SetTimer, aa, 60000 ; updates every 1 minute
aa:
formattime, thedate,,dd-MM-yyyy
IniRead, OutputVar, track.txt, %thedate%, mins, 0
if (A_TimeIdlePhysical<180000) {
OutputVar++
IniWrite, %OutputVar%, track.txt, %thedate%, mins
}
Return
FormatMinutes(NumberOfMinutes) ; Convert mins to hh:mm
{
time = 19990101 ; *Midnight* of an arbitrary date
time += %NumberOfMinutes%,minutes
FormatTime, mmss, %time%, H 'h' mm 'min'
return mmss
}
rctrl::
formatted := FormatMinutes(OutputVar)
gui, add, text, w150 center, Usage today: %formatted%
gui, -sysmenu
gui, show,, TinyTracker 1.02
Keywait,rctrl, D
Keywait,rctrl
gui, destroy
return
closeall:
exitapp
|
Edit: minor update to v1.02 with 2 fixes suggested by OceanMachine
Last edited by lazygeek on Thu Mar 05, 2009 4:17 am; edited 4 times in total |
|
| Back to top |
|
 |
Robbo
Joined: 27 Jul 2008 Posts: 44 Location: England
|
Posted: Wed Feb 25, 2009 11:14 pm Post subject: |
|
|
I'm still trying it out but I like this script, nice and easy and I think quite useful! Good one!  _________________ All scripts are untested unless otherwise mentioned. |
|
| Back to top |
|
 |
lazygeek
Joined: 08 Feb 2009 Posts: 16
|
Posted: Thu Feb 26, 2009 3:06 pm Post subject: |
|
|
Robbo, glad you like it.
The script should be quite stable,
but if you encounter any bugs, please report.
Thanks for your feedback |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Tue Mar 03, 2009 9:36 pm Post subject: |
|
|
Thanks, very useful.
I changed
| Code: | | menu, tray, icon, C:\WINDOWS\system32\shell32.dll, 44 |
to
| Code: | | menu, tray, icon, %A_WinDir%\system32\shell32.dll, 44 |
Because C:\ is not my system drive and so this should make it work whatever your system drive or name of windows folder (e.g. could be WINDOWS or WINNT or even user specified).
I also changed
| Code: | | if (A_TimeIdle<180000) { |
to
| Code: | | if (A_TimeIdlePhysical<180000) { |
So that any automated keystrokes or mouse clicks that my scripts may do while I am not there will not be counted as usage time.
But other than that it worked great, thanks very much for sharing it  |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Tue Mar 03, 2009 10:12 pm Post subject: |
|
|
I just added the ability to track idle time as well as active time, and also to log the total time (although this is obvious as it's just a sum of the two other numbers).
Hope you dont mind.
Not sure if anyone would find this useful, but thought I would post it anyway:
| Code: | Menu, Tray, Icon, %A_WinDir%\system32\shell32.dll, 44
Menu, Tray, NoStandard
Menu, Tray, Add, Exit, closeall
SetTimer, CheckTime, 60000 ; updates every 1 minute
CheckTime:
FormatTime, thedate,,dd-MM-yyyy
IniRead, ActiveTime, track.txt, %thedate%, active, 0
IniRead, IdleTime, track.txt, %thedate%, idle, 0
If (A_TimeIdlePhysical < 60000) ; edited this line as per below
ActiveTime++
Else
IdleTime++
TotalTime := IdleTime + ActiveTime
IniWrite, %ActiveTime%, track.txt, %thedate%, active
IniWrite, %IdleTime%, track.txt, %thedate%, idle
IniWrite, %TotalTime% , track.txt, %thedate%, total
Return
FormatMinutes(NumberOfMinutes) ; Convert mins to hh:mm
{
Time := 19990101 ; *Midnight* of an arbitrary date
Time += %NumberOfMinutes%,minutes
FormatTime, mmss, %time%, H 'h' mm 'min'
Return mmss
}
rctrl::
Gui, Add, Text, w150 Center, % "Active Time: " FormatMinutes(ActiveTime) "`nIdle Time: " FormatMinutes(IdleTime) "`nTotal: " FormatMinutes(TotalTime)
Gui, -SysMenu
Gui, Show
Keywait,RCtrl, D
Keywait,RCtrl
Gui, Destroy
Return
closeall:
ExitApp |
Thanks again for writing and posting the original script, very useful
Edit: changed If (A_TimeIdle < 180000) to If (A_TimeIdlePhysical < 60000) as per below posts.
Last edited by OceanMachine on Thu Mar 05, 2009 1:05 pm; edited 2 times in total |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Wed Mar 04, 2009 9:02 am Post subject: |
|
|
Case 1: machine runs more than 24 hours in a session
Case 2: script is (accidentally) closed and rerun or machine rebooted multiple times
What will this script display in any of the above situations?
Suggestions:
1. include a "session" section to count total active/idle time during current session
2. include a "today" section to count active/idle time for the current day, regardless of interruptions
3. modify the script to run in Win9x too (icon number may need to be changed too for 9x):
| Code: | A_SysDir := (A_OSType = "WIN32_NT") ? "32":""
A_SysDir := A_WinDir . "\System" . A_SysDir
Menu, Tray, Icon, %A_SysDir%\shell32.dll, 44
... | Could anyone post a screenshot of the XP icon so I could search for its 9x match?
The hotkey section needs to be modified too, since Win9x doesn't recognize RCTRL or any such L/R hotkey combination. Personally I set it to F6. |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Thu Mar 05, 2009 1:16 am Post subject: |
|
|
The ini file is used to track the times, and is updated as soon as the 1 minute timer is fired. So yes, if the script is closed or rerun on a machine, it won't matter. the most you would be out by in any 1 counter would be 1 minute.
Regarding your points 1-3:
1: If the machine runs for more than 24 hours in one session, the times will still be posted as the date on which the script was run in that session, so if you opened the script on 3rd march and left it on for 30 hours, the total time would show for 30 hours on 3rd march (this is because the thedate variable is defined when the script is loaded). So, there shouldn't be a need for a session timer as this already works if you dont replace or close the script in the session (after midnight on the following day at least).
2: Assuming you want 1: to be obeyed as well, then this would need extra counters in there to count for the day and the session separately.
3: The only thing making it different on win9x is the icon and the hotkey, right? Well these can easily be changed, it doesn't have to be exactly the same on all OSs Maybe it would be possible to use the built in variables A_OSType and/or A_OSVersion to detect this and set things accordingly?
To stop the script being accidentally rerun while it is already running, you can add the following in the script (I usually put these at the top):
| Code: | | #SingleInstance ignore |
The above directive will ignore the request to re-run the script, and just leave the old one running. Of course without this, the script will already ask you if you want to re-run the script and overwrite the currently running one anyway.
The icon is a little yellow star at the moment on XP, not sure if there is a similar one on Win9x? |
|
| Back to top |
|
 |
lazygeek
Joined: 08 Feb 2009 Posts: 16
|
Posted: Thu Mar 05, 2009 2:37 am Post subject: |
|
|
Glad to see this is generating some interest and nice suggestions .
I have amended the code in the original post with the 2 fixes suggested by OceanMachine. Thanks for this.
However, I will probably not be able to further update this script in the near future. |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Thu Mar 05, 2009 3:57 am Post subject: |
|
|
Hey OceanMachine, thanks for the details. Also thanks lazygeek for the initial script (should've said that in first post ).
Icon and hotkey apart, what I had in mind was basically a day counter for the current session. Something like:
Current session: 3 days 14 hours 25 minutes
Today's active: 4 hours 52 minutes
Today's idle: 0 hours 22 minutes
What I find odd is that the script has recorded 0 idle minutes, both yesterday and today (using OceanMachine's script version). I even lowered the idle check interval to 10,000 (10 sec) to no avail; there may be something wrong with that function under Win9x.
There's no yellow star in 98SE, not in shell32.dll at least. I'd settle for icon 25 instead, which is a file with an hourglass at the bottom-left. Or we could use one of the icons at famfamfam.com. |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Thu Mar 05, 2009 1:00 pm Post subject: |
|
|
Bear in mind that if you alter the timer value (from 60000) then you will also need to change the time interval check in the sub that is called by the timer. Additionally, changing this will mean that (for example) every 10 seconds your 'minute' counter would increase by one, so you would need to add some maths in there to only increment the minutes count if you were idle 6 times (if you had 10 seconds for the check frequency).
This made me notice that in my post with the idle timer modifications, I firstly didnt have A_TimeIdlePhysical in there (as per my original post in this thread - oops!), and secondly that the check is for 180000ms, not 60000ms as it should be for 1 minute.
@LazyGeek: you probably want to change | Code: | | If (A_TimeIdlePhysical<180000) | to | Code: | | If (A_TimeIdlePhysical<60000) | in your script otherwise you will only be recorded as not being active if you are idle for 3 minutes or more! ...unless there is something I am missing of course!
I have modified the script in my second post accordingly.
@Drugwash: With regard to the idle time not working:
| Quote: | From The AHK help File:
A_TimeIdle: ...This variable will be blank unless the operating system is Windows 2000, XP, or beyond... |
Sorry Drugwash, looks like there is no idle time for you using that built in variable under Win9x.
In fact, all this script will do under Windows 9x is record how many minutes the script has been open for on any given day (as every time the Timer sub is fired, A_IdleTime will be blank, hence it will always increment ActiveTime), not how long the computer has been in use for.
Sorry Drugwash, looks like this will not do what you need under Win9x, you may have to find a way to do this by detecting key presses or mouse movements and resetting a timer when you detect them or something like that? |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Thu Mar 05, 2009 1:19 pm Post subject: |
|
|
Yeah, figures. Also, it's funny that in a tool called AutoHotkey, hotkeys are the least usable (for 9x users), to begin with. Oh well...
Come to think about it, if the screensaver can detect activity, why the heck can't AHK do it too? Hmmm... food for thought (when my brains will stop boiling in own juice, that is).
See ya! |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Thu Mar 05, 2009 1:35 pm Post subject: |
|
|
| Quote: | | Come to think about it, if the screensaver can detect activity, why the heck can't AHK do it too? |
It can, its just that you have to do it another way, AHK doesn't provide a built-in variable that gives you that number to Win98 Users (assuming this is a limitation of the OS).
However you can 'manually' implement the same thing given a bit of work 
Last edited by OceanMachine on Thu Mar 05, 2009 7:51 pm; edited 1 time in total |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Thu Mar 05, 2009 1:48 pm Post subject: |
|
|
I know but that'd be Chris' job not mine...
I'll take a look at Miranda's idle detection routine... probably... someday.  |
|
| Back to top |
|
 |
OceanMachine
Joined: 15 Oct 2007 Posts: 140
|
Posted: Thu Mar 05, 2009 7:47 pm Post subject: |
|
|
That's just plain lazy...
It's not Chris's job at all, he has provided the platform upon which you can do what you like... if you should chose to use it to detect idle time, you can. He has been kind enough to provide a built in way to find the idle time on semi-recent operating systems... however not for those that went out with the ark, probably due to differences in the OS. Therefore you will have to do it in the same way that might approach any other problem and wish to solve it with AHK, and for which there isn't a nice easy built in variable... code it yourself
The vast majority of times we develop code there isn't a built in variable that gives you exactly what you need (in any platform), so the usual way is to generate that data yourself, not to start saying it is the developers job to provide you with a built in variable!
Good luck anyway  |
|
| Back to top |
|
 |
Drugwash
Joined: 07 Sep 2008 Posts: 608 Location: Ploiesti, RO
|
Posted: Fri Mar 06, 2009 11:27 pm Post subject: |
|
|
My point of view is that as long as there is a built-in function, it should work on all Win versions. At least that would be the kindest way to do things.
I can understand when due to many missing functions or libraries, there can't be overall compatibility (say, multi-monitor support for Win95/NT).
I've been working on a few scripts - mostly for personal usage - and although I'm running 98SE and couldn't care less about Win2000+, I've done my best (nagging neighbors and friends) to test and fix any issues that may appear while running under those OS versions. A little help in return would be welcome, IMHO.
Now, let's drop this silly argue and try to be constructive. I've promised I'd look into those Miranda sources and will do it, just as soon as I feel better after this bad cold. And I apologyze if any of my statements above looks rude - it's most likely the effect of the illness and medicine; don't wanna offend anyone.  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|