AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

tiny computer usage tracker
Goto page 1, 2  Next
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
lazygeek



Joined: 08 Feb 2009
Posts: 16

PostPosted: Wed Feb 25, 2009 5:20 pm    Post subject: tiny computer usage tracker Reply with quote

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
View user's profile Send private message
Robbo



Joined: 27 Jul 2008
Posts: 44
Location: England

PostPosted: Wed Feb 25, 2009 11:14 pm    Post subject: Reply with quote

I'm still trying it out but I like this script, nice and easy and I think quite useful! Good one! Smile
_________________
All scripts are untested unless otherwise mentioned.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
lazygeek



Joined: 08 Feb 2009
Posts: 16

PostPosted: Thu Feb 26, 2009 3:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Tue Mar 03, 2009 9:36 pm    Post subject: Reply with quote

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 Smile
Back to top
View user's profile Send private message
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Tue Mar 03, 2009 10:12 pm    Post subject: Reply with quote

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 Very Happy

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
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Wed Mar 04, 2009 9:02 am    Post subject: Reply with quote

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
View user's profile Send private message Yahoo Messenger
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Thu Mar 05, 2009 1:16 am    Post subject: Reply with quote

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 Smile 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
View user's profile Send private message
lazygeek



Joined: 08 Feb 2009
Posts: 16

PostPosted: Thu Mar 05, 2009 2:37 am    Post subject: Reply with quote

Glad to see this is generating some interest and nice suggestions Cool.

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
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Thu Mar 05, 2009 3:57 am    Post subject: Reply with quote

Hey OceanMachine, thanks for the details. Also thanks lazygeek for the initial script (should've said that in first post Embarassed ).
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. Confused

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
View user's profile Send private message Yahoo Messenger
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Thu Mar 05, 2009 1:00 pm    Post subject: Reply with quote

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! Smile ...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
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Thu Mar 05, 2009 1:19 pm    Post subject: Reply with quote

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... Sad

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
View user's profile Send private message Yahoo Messenger
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Thu Mar 05, 2009 1:35 pm    Post subject: Reply with quote

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 Smile


Last edited by OceanMachine on Thu Mar 05, 2009 7:51 pm; edited 1 time in total
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Thu Mar 05, 2009 1:48 pm    Post subject: Reply with quote

I know but that'd be Chris' job not mine... Twisted Evil

I'll take a look at Miranda's idle detection routine... probably... someday. Rolling Eyes
Back to top
View user's profile Send private message Yahoo Messenger
OceanMachine



Joined: 15 Oct 2007
Posts: 140

PostPosted: Thu Mar 05, 2009 7:47 pm    Post subject: Reply with quote

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 Smile

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 Very Happy
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 608
Location: Ploiesti, RO

PostPosted: Fri Mar 06, 2009 11:27 pm    Post subject: Reply with quote

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. Embarassed
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group