Discover The place You Waste Your Time on Home windows With AutoHotKey

You start each day with the best of intentions, rejuvenated and ready to tackle all of your tasks. Then you check the time, it's 3:00 AM and you can't help but wonder where has all this gone?

You could use a dedicated time tracking app, but these can be annoyingly complex. Besides, where's the fun in it?

Instead, why not create your own simple mini-app that records all of the windows you spend time in during the day? All you need is AutoHotKey, a simple word processor like Notepad, and about half an hour. Let's stick with it.

What is AutoHotKey?

The main purpose of AutoHotKey is desktop automation. It's a scripting language that lets you send keystrokes and mouse movements to any active window, create hotkeys, or change key presses. For example, you can use it to disable certain keys on your keyboard.

However, this is the short version of the story and not really representative of what AutoHotKey (AHK for short) can do. That's because it has evolved from its original conception and is now a full scripting language. You can think of what you can do with it as "Lite Programming".

MAKEUSEO VIDEO OF THE DAY

So you can also create mini apps with AHK, just as we have set ourselves the goal of this article. Note that we're going to dive right into AHK in this article. So maybe you should read our Quick Beginners Guide to AutoHotKey before you get started.

Create your own window logging script with AutoHotKey

Before we start, you should have AHK installed on your computer as it will act as a "parser" for your script. It is "the engine" that enables your script to "execute".

Note: You can also compile your script after it's done to turn it into an actual executable program. However, this is outside the scope of this article.

Download and install AutoHotKey from the official website.

Launch your favorite file manager and visit a folder where you want to save your script. Then right click on an empty space and choose New> AutoHotKey script.

Once that's done, it's time to write the actual script.

1. Define the required variables

Open the script in your favorite editor. You can use something as simple as Notepad that comes with Windows, but we'll use Notepad ++ for this guide. Since Notepad ++ is free and better tailored for this purpose, it's worth trying out. Also, check out our ultimate guide to all of the keyboard shortcuts while you try it out.

Note that you shouldn't use an app like Word or Google Docs as it could affect the formatting. Use either a text or "code" editor.

The script already includes some basics recommended for compatibility and performance. Leave them as they are and start your script underneath.

Start with:

AppLogging Rate = 10; Time interval (in seconds) between active window title captures.
Sleep time: = AppLoggingRate * 1000
LogPath =% A_ScriptDir%
LastActiveWindow =

We'll start by assigning AppLoggingRate the value "10", which we use to calculate the time between capturing window titles.

In connection with the sleep function of AHK, 1000 corresponds to approximately one second. By multiplying it by AppLogingRate, we make the SleepTime variable "equal to ten seconds".

LogPath is where we want to save our logs. We use the value% A_ScriptDir%, which translates as "the folder from which you run the script". You can use the full path to another folder if you want.

Finally, we set LastActiveWindow to empty. We'll use this later to see if the active window has changed.

2. Monitor the active Windows

Since we want to keep track of which window is active and when it changes, keep track of its title and time, we need to use "a loop".

As the name suggests, a loop runs continuously, repeating the same function (s). Thanks to the simple syntax of the AHK, we consider the following "code" to be relatively self-explanatory:

ribbon
{
Sleep% SleepTime%
Msgbox, it works!
}

We define a loop by simply entering the word "loop" and then marking the beginning with "{" and the end with "}". Everything in the lines between "{" and "}" continues to run until you exit the script.

We start the loop by waiting for a time (Sleep) that corresponds to the SleepTime variable. We set it as a variable in the previous chapter to make timing easier. Instead of editing the script itself, you can use this variable to "tell" it how many seconds each loop should last.

Finally, we'll use a message box to test our script. Try to save and run it (double click on the file). You will see a message box that says "It's working!" after ten seconds.

Right click the AHK icon in the Windows tray and exit the script when you have enough message boxes. Then go back to your editor and replace the MsgBox line with:

WinGetActiveTitle, ActiveWindow

This is the command to get the title of the active window. Ignore the additional line "StoreActiveWindow" that we used for testing while writing the script.

3. Get the current time and name

Now comes the core part of the logic of the script. We want it to compare the name of the active window with the previous one, and if they are different, "do something". It's as simple as this:

If ActiveWindow! =% LastActiveWindow%
{
}

With this we check whether the current ActiveWindow is different (! =) Than the value that is stored in the LastActiveWindow variable (which we initially set to empty). If this is the case, AHK executes the code between {and}, which are initially empty.

We need to keep track of both the date and the time to measure how long a window has been active. We keep different logs for each day and use the date on your behalf. And we don't just want to log every window change, but also when it happened. To do this, we assign different time formats to the LogTime and LogFilename variables, with:

FormatTime, LogTime ,, HH: mm: ss
FormatTime, LogFilename ,, yyyy-MMM-dd

Add these lines between the curly braces under "If ActiveWindow …" so that AHK can run them when it detects a window change.

4. Data formatting

We recorded the time in two differently formatted variables, as well as the title of the active window. However, there is a small problem: the title of a window can also contain characters that we don't need. We can remove all non-alphanumeric characters using AHK's support for RegEx, with:

LogWindow: = Regexreplace (ActiveWindow, "(^ a-zA-Z0-9)", "")

With this we "tell" AHK to remove all characters from the ActiveWindow variable that do not match the brackets:

  • lowercase letters

  • Capital letter

  • Counting

Then we assign the result to the LogWindow variable.

After all of the variables have been set and all of the valuable data has been captured, we are ready to format our log file and its contents.

LogFilename =% LogFilename% _AppLog.md
LogFile =% LogPath% % LogFilename%

We previously assigned the current date to the LogFilename variable. So with the first line we are only indicating that we want to add "_AppLog.md" to the date in order to use it as a file name.

In the second line we combine the LogPath variable, which we initially defined as the target for our logs, with the file name. Their combination is the full path name of the log that is assigned to the LogFile variable.

Let's assign the equivalent of "blank line, time – window name, two more blank lines, a divider and another blank line for safety" to the FileContent variable.

FileContent = `n% LogTime% -% LogWindow%` n`n- – -`n

  • The "` n "& # 39; s instruct AHK to enter a new line (corresponds to pressing the Enter key once).

  • The three dashes appear as separators when viewed in a Markdown compatible viewer.

  • "% LogTime%" and "% LogWindow%" are the variables in which we saved the name of the active window and the time of detection.

5. Update the file

We have defined what we want to write in our file and we know the path and the file name. What remains is the actual writing, which is as simple as:

FileAppend,% FileContent%,% LogFile%

It is almost as simple as in plain language: We append everything in the "FileContent" variable to the "LogFile" file.

The append function adds the FileContent to the file if it exists, but also creates it from scratch if it doesn't.

But wait, there is one final tweak: replace the contents of the LastActiveWindow variable with the currently active window.

This enables the script to recognize the next window change.

LastActiveWindow =% ActiveWindow%

And with this final addition, your window logger is ready! Save it and run it. Then check out the Markdown file, which after ten seconds will appear in the folder of your script file.

Master your time

You can open your log file with any text editor. Even so, it looks nicer when you open it up in a Markdown compatible editor. In the screenshot you can see our log in the popular Typora editor.

It's an easy way to check which apps you've been spending the most of your time, and all you need is something like Notepad to use it.

If you want something "more", you can always "style" the output of your logger to create CSV files instead. It's as simple as tweaking the FileContent variable and the extension of the created file. You can then import such files into apps like Excel, Google Calc, or even third-party time records.

Full script:

#NoEnv; Recommended for performance and compatibility with future AutoHotkey releases.
; #To warn; Enable warnings to identify common errors.
SendMode input; Recommended for new scripts because of its outstanding speed and reliability.
SetWorkingDir% A_ScriptDir%; Ensures a consistent start directory.
; variables
; ———
AppLogging Rate = 10; Time interval (in seconds) between active window title captures.
Sleep time: = AppLoggingRate * 1000
LogPath =% A_ScriptDir%
LastActiveWindow =
; logic
; —–
ribbon
{
Sleep% SleepTime%

WinGetActiveTitle, ActiveWindow
StoreActiveWindow =% ActiveWindow%

If ActiveWindow! =% LastActiveWindow%
{
FormatTime, LogTime ,, HH: mm: ss
FormatTime, LogFilename,, yyyy-MM-dd

LogWindow: = Regexreplace (ActiveWindow, "(^ a-zA-Z0-9)", "")

LogFilename =% LogFilename% _AppLog.md
LogFile =% LogPath% % LogFilename%

FileContent = `n% LogTime% -% LogWindow%` n`n- – -`n

sleep 50

FileAppend,% FileContent%,% LogFile%
LastActiveWindow =% ActiveWindow%
}
}
exit

10 cool AutoHotkey scripts (and how to make your own!)

AutoHotkey lets you create custom Windows shortcuts, macros, and more! Here are some useful AutoHotkey scripts to get you started.

Continue reading

About the author

Odysseas Kourafalos
(12 published articles)

OK's real life started at 10 when he got his first computer – a Commodore 128. Since then, he's melted keycaps by typing around the clock and trying to get the word of tech out to anyone interested enough to listen . Or rather read.

More
From Odysseas Kourafalos

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!

Click here to subscribe

Leave a Reply

Your email address will not be published. Required fields are marked *