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 

StopWatch (Time logging script)

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
Boskoop



Joined: 01 Jan 2005
Posts: 71

PostPosted: Mon Jul 11, 2005 6:27 pm    Post subject: StopWatch (Time logging script) Reply with quote

Time logging script. The clock starts with F11 and stops with F12. Reset by deleting or renaming the logfile.

The following times and intervals er written to a log file:
    Start Date
    Start Time
    Stop Time
    Duration this session (hh:mm:ss)
    Duration this session (seconds)
    Duration all sessions (hh:mm:ss)
    Duration all sessions (seconds)


Configuration:
It's possible to show "running" and "off" state of the clock with different icons. (Disabled in the posted script)
To change the appearance of the Log-File, modify the last line of the function Timelog().

I hope someone may find it useful. I myself wanted to log how long I am
waiting for a too-slow computer- but my boss promised me a new one
even without that log...

Boskoop



Code:

; ----------------------------------------------------------------
; Name:              StopWatch   
; Author:           Boskoop
; Datum:            25.6.05
;
; Language:         english
; Platform:         tested with XP
; AHK-Version:      1.0.35+
; #Includeable:     
;
; Description:                                               
; ---------------------------------------------------------------------
; Stop watch. Start it with F11, stop it with F12. Reset by deleting the
; LogFile.
; Writes Starttime, stoptime, actual intervall and cumulated time in a
; logfile
; ---------------------------------------------------------------------

; ---------------------------------------------------------------------
; -- Configuration: ---------------------------------------------------
; ---------------------------------------------------------------------
LogFile=BoskoopsTimelogExample.txt     ;path and name of the Logfile
;OnIcon=timerA.ico               ;name and path of the taskbar icon when timer on
;OffIcon=timerB.ico               ;name and path of the taskbar icon when timer of
OnTooltip=Timer On               ;Tooltip with clock running
OffTooltip=TimerOff               ;Tooltip with clock off

; ---------------------------------------------------------------------
; -- Initialize: ------------------------------------------------------
; ---------------------------------------------------------------------
State=TimerOff
Menu, tray, Icon,%OffIcon%
Menu, Tray, Tip,%OffTooltip%      ;ToolTip: Timer Status

; ---------------------------------------------------------------------
; -- Autoexecute ------------------------------------------------------
; ---------------------------------------------------------------------

F11::
StartTime=
StopTime=
StartTime=%A_Now%
State=TimerOn
Menu, tray, Icon,%OnIcon%
Menu, Tray, Tip,%OnTooltip%   
return

F12::
if state=TimerOff               ;checks if Timer is running before shutting it down
   return
State=TimerOff
StopTime=%A_Now%
TimeDiff=%StopTime%
EnvSub TimeDiff, %StartTime%, seconds         
Last=
IfExist %Logfile%
   {
   Last:=LastFieldInFile(LogFile)
   }
Actual=%Last%
EnvAdd Actual,%TimeDiff%,
TimeLog(StartTime,StopTime,TimeDiff,Actual,LogFile)
StartTime=
StopTime=   
Menu, tray, Icon,%OffIcon%
Menu, Tray, Tip,%OffTooltip%
return


   
; ---------------------------------------------------------------------
; -- Functions --------------------------------------------------------
; ---------------------------------------------------------------------
TimeLog(T1,T2,Sek,CumSek,File)         
;Formats Time values and appends them to the logfile
;T1:StartTime (as YYYYMMDDhhmmss) T2:StopTime (as YYYYMMDDhhmmss)
;Sek: Duration this session (in seconds),, CumSek: Duration all sessions (in seconds)
;File: Name and path of the logfile
{
   Stringmid, Y1,T1,1,4   ;Year
   Stringmid, M1,T1,5,2   ;Month
   Stringmid, D1,T1,7,2   ;Day
   Stringmid, h1,T1,9,2   ;hour
   Stringmid, min1,T1,11,2   ;minute
   Stringmid, s1, T1,13,2   ;second
   
   Stringmid, Y2,T2,1,4   ;Year
   Stringmid, M2,T2,5,2   ;Month
   Stringmid, D2,T2,7,2   ;Day
   Stringmid, h2,T2,9,2   ;hour
   Stringmid, min2,T2,11,2   ;minute
   Stringmid, s2, T2,13,2   ;second
   
   T1_Format=%D1%.%M1%.%Y1%  %h1%:%min1%:%s1%
   T2_Format=%h2%:%min2%:%s2%
   Sek_Format:=FormatSeconds(Sek)         ;This session (in seconds)
   CumSek_Format:=FormatSeconds(CumSek)   ;All sessions (in seconds)
   
   
   ;Modify this string to change the log-appearance. CumSek must alway be last, separated by a tab.:
   Fileappend, %T1_Format%%A_Tab%%T2_Format%%A_Tab%%Sek_Format%%A_Tab%%Sek%%A_Tab%%CumSek_Format%%a_Tab%%CumSek%`n,%File%
}
; ---------------------------------------------------------------------

FormatSeconds(Z1)
;converts seconds to hh:mm:ss 
;Z1: Time in seconds
{
   transform,S,MOD,Z1,60
   stringlen,L1,S
   if L1 =1
   S=0%S%
   if S=0
   S=00

   M1 :=(Z1/60)
   transform,M2,MOD,M1,60
   transform,M3,Floor,M2
   stringlen,L2,M3
   if L2 =1
   M3=0%M3%
   if M3=0
   M3=00
   
   H1 :=(M1/60)
   transform,H2,Floor,H1
   stringlen,L2,H2
   if L2=1
   H2=0%H2%
   if H2=0
   H2=00
   result= %H2%:%M3%:%S%
   return result
}

;----------------------------------------------------------------------
LastFieldInFile(Filename)
;Returns the content of the last tab-delimited field in the last line of a file.
{
   loop,read,%Filename%               ;Number of lines in file
   {
      ++LastLineNumber
   }
   FileReadLine, LastLine, %Filename%, %LastLineNumber%
   Loop, parse, LastLine, %A_Tab%         ;Number of fields in last line
   {
      ++LastFieldNumber   
   }
   Loop, parse, LastLine, %A_Tab%         ;Content of last field in last line
   {
      if A_Index = %LastFieldNumber%
         {
         LastFieldContent=%A_LoopField%
         }
   }
   return %LastFieldContent%
   
}

;---------------------------------------------------------------------

Quit:
   ExitApp
Return

Back to top
View user's profile Send private message
Guest






PostPosted: Tue Aug 09, 2005 3:35 pm    Post subject: Reply with quote

Thanks for the script Very Happy
By looking over your script I learned some thing new: how to use functions. I knew there was something called functions in autohotkey but never tried it.

I was able to make a stop watch, using one of your funciton to conver seconds in hh:mm:ss format

Code:
Gui, Add, Button, x6 y50 w50 h40, Start
Gui, color, black
Gui, Add, Button, x66 y50 w50 h40, Stop
Gui, Font, s20
Gui, Add, Text,cwhite x8 y10 w111 h30 vtext, 00:00:00
Gui, Show, x270 y110 h99 w124,
return

buttonStop:
stop:= 1 ; this line helps to break the loop under buttonstart
return

buttonStart:
stop:= 0
time1:= 0
loop
{
if stop = 1
{
break
}
sleep, 1000
time1:= time1 + 1 ; update time every second
var1 := Timelog(time1)
Guicontrol, ,text, %var1%
}
return




; -- Functions --------------------------------------------------------
; ---------------------------------------------------------------------
TimeLog(T1)
{
   transform,S,MOD,T1,60
   stringlen,L1,S
   if L1 =1
   S=0%S%
   if S=0
   S=00

   M1 :=(T1/60)
   transform,M2,MOD,M1,60
   transform,M3,Floor,M2
   stringlen,L2,M3
   if L2 =1
   M3=0%M3%
   if M3=0
   M3=00

   H1 :=(M1/60)
   transform,H2,Floor,H1
   stringlen,L2,H2
   if L2=1
   H2=0%H2%
   if H2=0
   H2=00
   result= %H2%:%M3%:%S%
   return result
}

esc:
GuiClose:
ExitApp



But it is not so accurate, and it pauses when you are moving the gui, any idea how to solve this problem

Also is it possible to change the color of buttons?
Back to top
tpatel5



Joined: 27 Oct 2004
Posts: 65
Location: GA

PostPosted: Tue Aug 09, 2005 3:53 pm    Post subject: Reply with quote

I modified my script to use the a_now insted, to have more accuracy. and it has also solved my moving problem Wink But still can't change the color of the buttons Sad
so here is the modified script:

Code:

Gui, +owner
Gui, color, Black
Gui, Add, Button, x36 y150 w60 h40, Start
Gui, Add, Button, x106 y150 w60 h40, Stop
Gui, Show, x276 y110 h200 w207, Clock & Stop Watch
Gui, Font, s20 cwhite
Gui, Add, Text, x36 y10 w130 h40 center vctext,
Gui, Font, s15
Gui, Add, Text, x6 y60 w190 h30 center vtext1,
Gui, Add, Text, x46 y100 w110 h30 center vtext2,
loop
{
sleep, 1000
ctime =
formattime, ctime, , hh:mm:ss
Guicontrol, ,ctext, %ctime% ;display the current time for the clock
}
Return

ButtonStart:
t1:= A_now
stime:= t1
formattime, stime, stime, hh:mm:ss
Guicontrol, ,text1, %stime% ; display the starting time
Send, {tab}
Return

ButtonStop:
t2:= A_now
etime:= t2
formattime, etime, etime, hh:mm:ss
Guicontrol, ,text1, %stime%  %etime% ; display the staring time and end time
t2 -= t1, s
z:= timediff(t2)
Guicontrol, ,text2, %z%
send, {tab}
Return


;Function to convert seconds in to hh:mm:ss format
timediff(st)
{
   transform,S,MOD,st,60
   stringlen,L1,S
   if L1 =1
   S=0%S%
   if S=0
   S=00

   M1 :=(st/60)
   transform,M2,MOD,M1,60
   transform,M3,Floor,M2
   stringlen,L2,M3
   if L2 =1
   M3=0%M3%
   if M3=0
   M3=00

   H1 :=(M1/60)
   transform,H2,Floor,H1
   stringlen,L2,H2
   if L2=1
   H2=0%H2%
   if H2=0
   H2=00
   result= %H2%:%M3%:%S%
   return result
}
Return

esc::ExitApp
GuiClose:
ExitApp

_________________
-Tru Cool
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
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