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!