Simple Sobriety Tracker

Post your working scripts, libraries and tools
RLocke
Posts: 2
Joined: 17 May 2018, 16:08

Simple Sobriety Tracker

17 May 2018, 16:30

I decided to get my drinking under control before anything got out of hand, and made this Sobriety Tracker. Thought i'd pass it on in case anyone else could use it.
Image

It creates a widget on the desktop that tracks your sobriety (based on the date at the top of the file). It keeps up with days, weeks, months, and years. It also dynamically resizes to fit the content in the widget, can be click and dragged, and can be closed via the notification tray.

New versions can be found on pastebin first, unless there's much interest on this forum: https://pastebin.com/mH9WhZGK

To Do: maybe add some random quotes, in gui-configuration, & maybe track statistics over time if the count ever resets.

Code: Select all

/*
	Version:
	0.46 Update the Desktop Widget without making it show on top of anything else.
	0.45 Added more user editable values at the top of the file
	0.44 Show "it's a new day" for either the start of a new week, or someones first day.
	0.43 added dynamic width calculation. I think that can be done via the gui itself, but it's less fun that way.
	0.42 removed the taskbar button, now it has to be closed from the notification tray.
	0.41 made the window transparent, dynamically resizes a bit, tracks weeks, removed borders/title bar.
*/

	start_date = 20180505 ; Date in a YYYYYMMDD format.
	menu_X := 435 ;x cordinate for where the menu loads
	menu_Y := 20 ; y coord

	gosub load_gui ; end user editable variables
	gosub update_gui
	SetTimer, update_gui, 900000 ; update every 10 minutes
return

load_gui:
	Gui, Color, Black
	Gui +Owner ; hide taskbar button, it must be closed from the notification tray.

	Gui, Font, s10, Trebuchet MS
	Gui, Add, Text, x2 y2  h200 w200 vDays cwhite ;cf4f3f5,

	Gui, Show, x%menu_X% y%menu_Y% h80 w125,

	Gui +LastFound  ; Make the GUI window the last found window for use by the line below.
	WinSet, Transparent, 150, A

	;Gui, Color, EEAA99 ;this and the line below allows full transparency
	;WinSet, TransColor, EEAA99

	WinSet, Style, ^0xC00000, A ; Hide the title bar and borders
	OnMessage(0x201, "WM_LBUTTONDOWN") ; allow the menu to be dragged by the font. Ouch.
return

WM_LBUTTONDOWN()
{
	PostMessage, 0xA1, 2
}

uiMove:
	PostMessage, 0xA1, 2,,, A
Return

GuiClose:
	ExitApp
return

update_gui:
	FormatTime, m_days, YYYYMMDD, yyyyMMdd
	EnvSub, m_days, %start_date%, days

	;m_days := 725 ; testing purposes

	m_totaldays := m_days
	m_includetotal := ( m_days >= 7 )

	m_string := ""
	if ( m_days >= 365 )
		m_string := m_string . "Years sober: " Floor( m_days / 365 ) . "`n"

	while m_days >= 365
		m_days -= 365

	if ( m_days >= 30 ) ; average days in a month is a smidge over 30
		m_string := m_string . "Months Sober: " Floor( m_days / 30 ) . "`n"

	while m_days >= 30
		m_days -= 30

	if ( m_days >= 7 )
		m_string := m_string . "Weeks Sober: " Floor( m_days / 7 ) . "`n"

	while m_days >= 7
		m_days -= 7

	if ( m_Days > 0 )
		m_string := m_string . "Days Sober: " m_days . "`n"
	else
		m_string := m_string . "It's a new day!`n"

	if ( m_includetotal )
		m_string := m_string . "`nTotal days sober: " m_totaldays

	GuiControl, Text, Days, %m_string%

	StringReplace, m_string, m_string, `n, `n, UseErrorLevel
	height := ( ErrorLevel *= 24 ) ; 25 pixels for every 2 lines
	;width := ( m_totaldays >= 1000 ? 137 : 127 ) ; just add 10 pixels for the thousandths collumn, I don't care all that much about tens of thousands.
	width := ( StrLen( m_totaldays ) >= 4 ? 127 + ( ( StrLen( m_totaldays ) - 3 ) * 8 ) : 127 ) ; I lied.

	Gui, Show, x%menu_X% y%menu_Y% h%height% w%width% NA,
return

IsWhole(Number)
{
	return !Mod(Number, Floor(number))
}

iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: Simple Sobriety Tracker

22 May 2018, 17:26

Hi RLocke,

I was inspired by your script so I generalized it and added some functions from the old forum to calculate the elapsed periods:

Code: Select all

start_date := A_Now

Gosub, Load_Gui
SetTimer, Update_Gui, 1000
Gosub, Update_Gui
Gosub, Add2Menu
Return

Add2Menu:
   Menu, Tray, Add
   Menu, Tray, Add, Show Window, Show
   Menu, Tray, Default, Show Window
   Menu, Tray, Click, 1
Return

Show:
   Gui, Show
Return

Load_Gui:
   Gui, +LastFound +Owner
   Gui, Margin, 5, 5
   Gui, Color, Black
   Gui, Font, s10, Trebuchet MS
   Gui, Add, Text, cwhite vDays, Total seconds:`t000
   GuiControlGet, T, Pos, Static1
   
   WinSet, Transparent, 150
   WinSet, Style, ^0xC00000 ; Hide the title bar and borders
   OnMessage(0x201, "WM_LBUTTONDOWN")
Return

WM_LBUTTONDOWN() {
   PostMessage, 0xA1, 2
}

Update_Gui:
   Age := GetAge(start_date, A_Now)
   Years   := SubStr(Age, 1,4)+0
   Months  := SubStr(Age, 5,2)+0
   Days    := SubStr(Age, 7,2)+0
   Hours   := SubStr(Age, 9,2)+0
   Minutes := SubStr(Age,11,2)+0
   Seconds := SubStr(Age,13,2)+0
   Weeks := Days//7, Days -= Weeks*7
   
   m_string := ""
   if Years
      m_string .= "Years:`t`t" Years "`n"
   if Months
      m_string .= "Months:`t`t" Months "`n"
   if Weeks
      m_string .= "Weeks:`t`t" Weeks "`n"
   if Days
      m_string .= "Days:`t`t" Days "`n"
   if Hours
      m_string .= "Hours:`t`t" Hours "`nMinutes:`t" Minutes "`nSeconds:`t" Seconds "`n"
   else if Minutes
      m_string .= "Minutes:`t" Minutes "`nSeconds:`t" Seconds "`n"
   else if Seconds
      m_string .= "Seconds:`t" Seconds "`n"
   else
      m_string .= "Seconds:`t0`n"
      
   if (Total := DateDIFF(start_date, A_Now, "D"))
      m_string .= "`nTotal days:`t" Total
   else if (Total := DateDIFF(start_date, A_Now, "H"))
      m_string .= "`nTotal hours:`t" Total
   else if (Total := DateDIFF(start_date, A_Now, "M"))
      m_string .= "`nTotal minutes:`t" Total
   else if (Total := DateDIFF(start_date, A_Now, "S"))
      m_string .= "`nTotal seconds:`t" Total
   else
      m_string .= "`nTotal seconds:`t0"
   
   StrReplace(m_string, "`n", "`n", Count)
   GuiControl, , Days, % m_string
   GuiControl, Move, Days, % "h" TH*(Count+1)
   Gui, Show, AutoSize NA
Return

; https://autohotkey.com/board/topic/19759-calculating-age-from-two-datetime-stamps/page-3#entry130627

GetAge(B,E) {
   Y:=M:=D:=0, Feb29 := SubStr(B,1,4) "0229235959"
   dYear := DateDiff(Feb29,B) > 0 ? 365 * 86400 : LDOY(B) * 86400
   if DateDiff(B,E) >= dYear
      B:=DateAdd(B,dYear), Y++
   Loop
      if DateDiff(B,E) >= (sYear := LDOY(B) * 86400)
        B:=DateAdd(B,sYear), Y++
      else Break
   Loop
      if DateDiff(B,E) >= (sMonth := LDOM(B) * 86400)
        B:=DateAdd(B,sMonth), M++
      else Break
   Loop
      if DateDiff(B,E) >= 86400
        B:=DateAdd(B,86400), D++
      else Break
   Remainder := DateAdd("16010101",DateDiff(B,E))
   FormatTime, HMS, %Remainder%, HHmmss
   Return Format("{:04u}{:02u}{:02u}",Y,M,D) HMS
}

DateDiff(B,E,U:="S") {  ; EnvSub
   E-=%B%,%U%
   Return E
}

DateAdd(Dt,V,U:="S") {  ; EnvAdd
   Dt+=%V%,%U%
   Return Dt
}

LDOY(Dt) {              ; Last Day of Year
   Year:=SubStr(Dt,1,4)
   Days:=Year+1
   Days-=%Year%,D
   Return Days
}

LDOM(Dt) {              ; Last Day of Month
   M1:=SubStr(Dt,1,6)
   M2:=M1
   M2+=31,D
   M2:=SubStr(M2,1,6)
   M2-=%M1%,D
   Return M2
}
Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: Simple Sobriety Tracker

23 May 2018, 10:24

Love this!
RLocke
Posts: 2
Joined: 17 May 2018, 16:08

Re: Simple Sobriety Tracker

25 May 2018, 01:31

iPhilip wrote:Hi RLocke,

I was inspired by your script so I generalized it and added some functions from the old forum to calculate the elapsed periods
Awesome thanks, I like your take on it and that can even double as a quick stop watch. I thought I remembered an auto-size option for the GUI, but mathing that out can also be half the fun. lol
User avatar
SpeedMaster
Posts: 228
Joined: 12 Nov 2016, 16:09

Re: Simple Sobriety Tracker

25 May 2018, 03:08

RLocke wrote: It keeps up with days, weeks, months, and years.
Correct me if I am wrong but this script can not sustain a pc reboot. Any idea of an ini file ?
kunkel321
Posts: 166
Joined: 30 Nov 2015, 21:19
Facebook: https://www.facebook.com/kunkel321
Google: kunkel321

Re: Simple Sobriety Tracker

25 May 2018, 07:52

SpeedMaster wrote:Correct me if I am wrong but this script can not sustain a pc reboot. Any idea of an ini file ?
Normally I'd agree with the flexibility of an ini file, but for this particular application (sobriety), I think that having the date "hard coded" might be more appropriate(?)
User avatar
SpeedMaster
Posts: 228
Joined: 12 Nov 2016, 16:09

Re: Simple Sobriety Tracker

25 May 2018, 17:14

start_date := A_Now :arrow: Ok, this assertion will not support a restart of the PC. :facepalm:
I tested both scripts with USA Independence Day start_date := 17760704 (July 4, 1776).
iPhilip's script gives a more accurate result but does not give the total number of days. :think:

http://www.mycalculators.net/utility/day-counter
iPhilip
Posts: 293
Joined: 02 Oct 2013, 12:21

Re: Simple Sobriety Tracker

25 May 2018, 18:48

Hi SpeedMaster,

With respect to the use of an ini file, you can definitely do that if you want to. As kunkel321 pointed out, because that date won't change that often (hopefully! ;) ), it's perhaps more work than necessary. The script file can act as the source of the data.

Thank you for testing it with such a fun date (July 4, 1776) :dance:. The reason why my script doesn't show the total number of days is because I only allocated 3 digits for that field in this line in the script (which represents the widest field in the Gui):

Code: Select all

Gui, Add, Text, cwhite vDays, Total seconds:`t000
If you add two more zeros at the end of the line,

Code: Select all

Gui, Add, Text, cwhite vDays, Total seconds:`t00000
the number of days will display properly (88348). I wasn't anticipating starting the count that far back. :lol:

Cheers!
Windows 7 Pro (64 bit) - AutoHotkey v1.1+ (Unicode 32-bit)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: hoppfrosch and 27 guests