[AHK] Semi-automatic time-tracking based on the content of a window's title bar

A place to post your ideas and solutions for how best to make use of the software

Moderators: abstr, Niko

Post Reply
Niko
Posts: 239
Joined: Wed Sep 18, 2019 6:48 am

[AHK] Semi-automatic time-tracking based on the content of a window's title bar

Post by Niko » Thu Jan 09, 2020 7:38 pm

December was a really busy month for me, I hope in January I would have some more free time to write something useful for ToDoList =)
The inspiration for this script was...my laziness: I had to constantly switch between ToDoList and other software start tracking one activity, than another many, many times. I managed that the solution could be the following:
1. There are several tasks which are used to accumulate the tracking time (Drawing "112", Reading "113").
2. There is software I use which corresponds to the tasks from 1. (let's say, to draw a picture in Krita, or Kindle to read books)
3. Each software from 2. has a unique title, like "New document.png - Krita", or "Therese Raquin - Kindle"
4. When a hotkey is pressed and the content of the window's title bar from 3. = the content of the script below the following happens:
- the Task ID (113) is selected in ToDoList
- the timer for 113 is being set
The Code:

Code: Select all

^+l::
{
 WinGetActiveTitle, Title_window
 If InStr(Title_window, "Therese Raquin / Or any other text you want to match")
 {
  Run C:\{THE PATH TO TDL}\ToDoList.exe /$(seltid)  -tid 113
  Sleep, 300
  Run C:\{THE PATH TO TDL}\ToDoList.exe /$(seltid)  -cmd 32967
  return
 }
}
Where
^+l:: - ctrl-shift-l - a hotkey
-tid 113 - ID of the Task you want to track
-cmd 32967 - The ID to start Time-tracker

I'll add animation a little bit later.

didi
Posts: 33
Joined: Fri Jun 12, 2020 2:29 pm

Re: [AHK] Semi-automatic time-tracking based on the content of a window's title bar

Post by didi » Thu May 20, 2021 10:51 am

Hi Niko and everyone interested,

Great script you got there, well done. All the same, and I hope and intend no offense, but I think this solution might be better.
since not so long MS released a free version of their automation tool, Power Automate Desktop (for to run on PC, not the cloud stuff).
I just managed to create a (socalled) Flow that will automatically recognize what Windows is in the foreground, match the title against a list of keywords and if there is a match, start tracking the associated task ID in TDL. The way the flow is at the moment, you have to manually insert/change this list of keywords and task IDs within the flow, it should also be possible to do it through an external Excel sheet, eg. But I don't really know yet how you'd do that, but all the same it's not so difficult to do it this way.
The list allows for various keywords, so for example you could put two different words, like "Excel" and "weekly", and another "Excel" and "yearly" and associate different task IDs to each. If you open Excel but the file doesn't contain either of those two other words, nothing will happen. If another window receives the focus, then at once the tracking stops, until a matching foreground window is found.
So the list is obviously (for those who knwo some coding) nested arrays, an array for each association, of which the first item is the task ID, and then another array containing the keyword(s). It's an array even if it's just one keyword. Obviously the flow will check for matches in the order that the items are entered, and so if you had one with just "Word", and later another with "Word" and a second keyword, then you'd never get to the second item/association, because the one "Word" will always match, whatever Word doc you open, so you should put that last, or in any case after the combination of Word and ...
Unfortunately this free version doesn't have, for as much as I know and understand, a function to automatically run at boot, but there might be something to have it run through the Windows Tasker thing...

Here's the link to the software
https://flow.microsoft.com/en-us/desktop/

Here's the code for this flow:

Code: Select all

SET WindwosTitlesToTrack TO {['58', ['zondag', 'Word']], ['612', ['Chrome']] }
SET Tracking TO 0
LABEL Begin
UIAutomation.Windows.GetForegroundWindow WindowTitle=> WindowTitle WindowInstance=> AutomationWindow
LOOP FOREACH CurrentTitle IN WindwosTitlesToTrack
    SET Found TO 1
    LOOP FOREACH Titles IN CurrentTitle[1]
        IF NotContains(WindowTitle, Titles, True) THEN
            SET Found TO 0
            EXIT LOOP
        END
    END
    IF Found = 1 THEN
        System.RunApplicationAndWaitToComplete ApplicationPath: $'''C:\\TDL\\ToDoList.exe''' CommandLineArguments: $'''/$(seltid)  -tid %CurrentTitle[0]%''' WindowStyle: System.ProcessWindowStyle.Hidden Timeout: 60 ExitCode=> AppExitCode
        WAIT 3 
        System.RunApplicationAndWaitToComplete ApplicationPath: $'''C:\\TDL\\ToDoList.exe''' CommandLineArguments: $'''/$(seltid)  -cmd 32967''' WindowStyle: System.ProcessWindowStyle.Hidden Timeout: 60 ExitCode=> AppExitCode
        SET Tracking TO 1
        UIAutomation.Windows.FocusByInstanceOrHandle WindowInstance: AutomationWindow
        EXIT LOOP
    END
END
BLOCK WaitLoseFocus
ON BLOCK ERROR

END
    WAIT (UIAutomation.Windows.ToLoseFocusByInstanceOrHandle WindowInstance: AutomationWindow) 
END
IF Tracking = 1 THEN
    System.RunApplicationAndWaitToComplete ApplicationPath: $'''C:\\TDL\\ToDoList.exe''' CommandLineArguments: $'''/$(seltid)  -cmd 32967''' WindowStyle: System.ProcessWindowStyle.Hidden Timeout: 60 ExitCode=> AppExitCode
    SET Tracking TO 0
END
GOTO Begin
You can just create a new flow, give it any name, and then copy and paste this text into the window. That seems to be the only/easiest way to share flows in the free version...

What you need to change:
-you'll have to change the list of ids and keywords that here is just an example
-The path to the ToDoList.exe on your PC in three blocks!

Like I said, I basically just worked this out in a day, with little testing, and it works on my machine, not necesarily on others... So if it doesn't work, or my explanation here wasn't clear enough, let me know, we'll work something out...

Hope this will be for someone's use!

Post Reply