You could use a dedicated time-tracking app, but those can be annoyingly complex. Plus, where’s the fun in that?

Why not make your own simple mini-app instead, which could track all the windows you’ve spent time on during the day? You’ll only need AutoHotKey, a basic word processor like Notepad, and about half an hour. Let’s get stuck in.

What Is AutoHotKey?

AutoHotKey’s primary purpose is desktop automation. It’s a scripting language with which you can send keystrokes and mouse movements to any active window, create hotkeys, or modify keypresses. You can, for example, use it to disable specific keys on your keyboard.

However, that’s the short version of the story and not truly representative of what AutoHotKey (AHK for short) can do. That’s because it has evolved since its initial conception and now is a complete scripting language. You can think of what you can do with it as “programming Lite.”

Thus, you can also use AHK to create mini-apps, precisely like what we set as our goal for this article. Note that in this article we’ll be diving right into AHK, so, you might want to check our quick AutoHotKey Guide for Beginners before you get started.

Creating Your Own Window-Logging Script With AutoHotKey

Before we begin, you should have AHK installed on your computer since it will act as the “parser” for your script. It’s “the engine” that will allow your script “to run”.

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

Download AutoHotKey from its official site and install it.

Fire up your favorite file manager, and visit a folder where you want to store your script. Then, right-click on an empty spot, and choose New > AutoHotKey Script.

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

1. Define the Necessary Variables

Open the script in your favorite editor. You can use something as simple as Notepad that comes with Windows, but we’ll be using Notepad++ for this guide. Since Notepad++ is free and better-tailored for the purpose, it’s worth trying it out. Make sure to also check our ultimate guide on all its keyboard shortcuts while you’re checking it out.

Note that you shouldn’t use any app like Word, or Google Docs, which could affect its formatting. Use either a text or “code” editor.

The script will already contain some basics recommended for compatibility and performance. Leave them as they are, and start your script under them.

Start with:

We begin by assigning the value “10” to AppLoggingRate, which we’ll use to calculate the time between capturing window titles.

When used with AHK’s Sleep function, 1000 is roughly equal to a second. So, by multiplying it with AppLogingRate, we’re making the variable SleepTime “equal to ten seconds”.

LogPath is the path where we want to store our logs. We’re using the value %A_ScriptDir%, which translates to “the folder from where you run the script”. You can use the full path to another folder if you like.

Finally, we set LastActiveWindow to blank. We’ll use this later to check if the active window has changed.

2. Monitor the Active Windows

Since we want to continuously keep track of which window is active, and if it changes, log its title and time, we’ll have to use “a loop”.

As its name states, a loop runs continuously, repeating the same function(s). Thanks to AHK’s straightforward syntax, we believe the following “code” is relatively self-explanatory:

We define a loop by simply typing the word “loop” and then marking its beginning with “{” and end with “}”. Everything in the lines between “{” and “}” will run perpetually until you exit the script.

We begin the loop by waiting (Sleep) for an amount of time equal to the variable SleepTime. We set it as a variable in the previous chapter to make controlling time more straightforward. Instead of editing the script itself, you can “tell” it, through this variable, how many seconds each loop should last.

Finally, we use a Message Box to test our script. Try saving and running it (double-click on its file). You’ll see a message box stating “It Works!” after ten seconds.

Right-click on AHK’s icon in the Windows tray and exit the script when you’ve had enough message boxes. Then, return to your editor, and replace the MsgBox line with:

This is the command for getting the active window’s title. Ignore the extra “StoreActiveWindow” line, which we used while writing the script for testing.

3. Get the Current Time & Name

Now comes the core part of the script’s logic. We want it to compare the active window’s name to the previous one, and if they’re different, “do something”. It’s as simple as the following:

With the above, we check if the currently ActiveWindow is different (!=) than the value stored in the variable LastActiveWindow (which we’ve initially set to blank). If it is, AHK will execute the code between { and }, which for now are empty.

We need to keep track of both the date and time to measure how long a window has been active for. We’ll keep different logs for each day, using the date in their name. And we want to log not only every window change but also when it happened. For that, we’ll assign different time formats to the variables LogTime and LogFilename, with:

Add those lines between the curly brackets under “If ActiveWindow…”, to have AHK run them when it detects a window change.

4. Data Formatting

We’ve grabbed the time in two differently formatted variables, as well as the active window’s title. However, there’s a tiny problem: a window’s title might also contain characters we don’t want. We can remove all non-alphanumeric characters using AHK’s support for RegEx, with:

With this, we “tell” AHK to remove all characters from the ActiveWindow variable that don’t match what’s in the brackets:

Lowercase letters Uppercase letters Numbers

Then, we assign the result to the variable LogWindow.

With all variables set and all valuable data grabbed, we’re ready to format our log file and its contents.

We previously assigned the current date to the LogFilename variable. Thus, with the first line, we’re merely stating that we want to add “_AppLog.md” to the date to use it as a filename.

In the second line, we combine the variable LogPath, which we defined at the beginning as the destination for our logs, with the filename. Their combination is the full pathname of the log, assigned to the LogFile variable.

Let’s assign the equivalent of “empty line, Time - Window’s Name, two more empty lines, a divider, and another empty line, for good measure” to the FileContent variable.

The “`n”’s tell AHK to enter a new line (the equivalent of pressing Enter once). The three dashes will appear as a divider when presented in a markdown-compatible viewer. “%LogTime%” and “%LogWindow%” are the variables where we’ve stored the active window’s name and the time it was detected.

5. Update the File

We’ve defined what we want to write to our file, and we know its path and filename. All that remains is the actual writing, which is as easy as:

It’s almost as straightforward as plain English: we append everything in the “FileContent” variable to the file “LogFile”.

The “append” function will add the “FileContent” to the file if it exists but will also create it from scratch if it doesn’t.

But wait, there’s one last tweak: replacing the LastActiveWindow variable’s content with the currently active window.

With this, the script will be able to detect the next window change.

And with that last addition, your window logger’s ready! Save it, and run it. Then, check out the markdown file, which will appear in your script file’s folder after ten seconds.

Master Your Time

You can open your log file with any text editor. Still, it will look prettier if you open it 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 on which apps you’ve spent the most time, and you only need something like Notepad to use it.

If you’d like something “more”, you can always “style” your logger’s output to produce CSV files instead. It’s as easy as tweaking the FileContent variable and the created file’s extension. You could then import such files into apps like Excel, Google Calc, or even third-party time trackers.

Full script: