Run an AHK script on a specific window only

109 0

This article will demonstrate how to run an AHK script on a specific window only, not just the window in focus. In other words, the script will perform its function on a specified window/program even if you are working in another program.

Our Use Cases

I will be showing 2 different ways to do this depending on your needs. This guide assumes you already have AHKv2 installed. If not, you can get it here: For this example, I have opened Adobe Premiere Elements 2020. Now I will open Window Spy for AHKv2, which is part of the AHK download. You can simply type “Window Spy” in your search bar to find and open it. Place it over whatever program you opened and place your cursor somewhere in that application. You will now see information about that program show up in the Window Spy application. Here is a screenshot of my example:

Example One

The very first line in the box is the Title of the window for Adobe Premiere Elements 2020. The 3rd line is the .exe for it. These are the two things we will be using for each example. Below is a script example for using the first option, which will be the .exe of the program. The script is a simple loop that simulates pressing the ‘v’ key of the keyboard every 3 seconds and then stops when you press the ESC key. You could just use send instead of ControlSend but when you move focus to another window it will be performing the script function against that window and not the intended one. This is a very basic script written just as an example. You of course would replace this with your own.

theWinExe:="ahk_exe PremiereElementsEditor.exe"
loop {

ControlSend,, v, %theWinExe%
sleep 3000
Esc::ExitApp ; Exit script with Escape key

So now when you run the script it will send the v key every 3 seconds; stopped by pressing the ESC key. This will happen only in the window of the .exe specified in your script, even when you are in a browser searching the internet or in your mail client reading mail.

Example Two

The second option is using the title in the window of a program. The reason you might want to use the title is if you have more than one instance of an application running. In this example I will open 2 instances of notepad and save them with different names. These saved names will be their window titles as seen below:

You will notice that the .exe is the same for both notepad files. If we were to use that, like in the previous example, we would not be able to run them at the same time as it would only work in one.  Now you will need a script for each note you want to perform some script function on. Again, we will use the same simple loop of entering the letter ‘v’ until the ESC key is pressed.

oneWinTitle:="NoteOne.txt - Notepad"
loop {

ControlSend,, v, %oneWinTitle%
sleep 3000
Esc::ExitApp ; Exit script with Escape key

And the other:

twoWinTitle:="NoteTwo.txt - Notepad"
loop {

ControlSend,, v, %twoWinTitle%
sleep 3000
Esc::ExitApp ; Exit script with Escape key

Final Thoughts

Here is the syntax for using ControlSend

ControlSend , Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText

I did not find much information online for a script running in a specific window or even multiple instances of a program. I thought this might be helpful information to others looking to do something similar. Feel free to ask questions and I will help if I can. If you are looking for a way to use your scripts as macros here is a nice article on using an old tablet to accomplish this.

Find more on

Total 0 Votes

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

About The Author

Coolest hedgehog in town!

No Comments on "Run an AHK script on a specific window only"

Leave a Comment

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