HOWTO: Use Autohotkey to Toggle Windows Based on Window ID

Autohotkey is an invaluable tool for automating tasks we perform hundreds of times per day without even thinking of it.  The tasks many only take a few seconds each, but added up over the course of a year or more, it really adds up!  One such common task involves switching between applications in Windows.  Sure, you can click the application you want on the task bar or use Alt-Tab. But what happens when you have a ton of programs and tabs open?

 

You have to hunt.

 

Wouldn’t it be great if you could just press a keyboard shortcut to launch/resume any application?  This is where Autohotkey excels.  You can configure it for example to toggle between applications based on their window title.  But what if you can’t use the window title?  This post shows you how to toggle between an application where the title constantly changes and you otherwise have nothing consistent about the application with which to configure Autohotkey to use.

In this example, I use two websites constantly throughout the day and I constantly have to switch back and forth to them.  The trouble is, due to how the web sites work, none of the conventional approaches I’ve found for toggling them on and off worked.  Thus I had to figure out my own approach.  Here is what I came up with:

 

Theory

1) There are two things that are guaranteed to be unique between each launched web browser, provided they are executed in their own processes – the process ID and the resulting HWND or Window ID

2) However since these change on each execution of the process, we need to make Autohotkey the authority for launching these web applications and ensure that each webapp launches in its own process.  That is to say we can never launch the websites on our own if we want to use the shortcuts.  But that’s fine since we leave them open all the time anyway.

 

Implementation

My approach is to break the process into two steps.  The first step is to create a shortcut that will only need to be run typically once per boot.  This automatically launches two instances of a web browser, each with their own process ID.

Next we configure a pair of hotkeys where Autohotkey that use the window IDs found earlier to minimize and maximize the desired window.

Prerequisites

This approach uses Firefox.  It turns out the only way to get Firefox to truly launch unique instances is if you create a unique profile first for each Instance.  You can do that by running:

“c:\Program Files (x86)\Mozilla Firefox\firefox.exe” –ProfileManager

Create a profile for each unique web application / website you intend to assign a shortcut to.

The Code

Copy the code below into your autohotkey.ahk file and replace the bold text with the information that is relevant for your use case.

^!z::
Run c:\Program Files (x86)\Mozilla Firefox\firefox.exe -no-remote –P Profile1 https://website1.com ,,,firefoxPID
WinWait, ahk_pid %firefoxPID%
WinGet,Profile1ID, ID, ahk_pid %firefoxPID%
WinMaximize
WinMinimize

Run c:\Program Files (x86)\Mozilla Firefox\firefox.exe -no-remote –P Profile2 https://website2.com ,,,firefoxPID
WinWait, ahk_pid %firefoxPID%
WinGet, Profile2ID, ID, ahk_pid %firefoxPID%
WinMaximize
WinMinimize

return

#a::
if !Profile1ID
{
msgbox 16, Error, Profile1ID is empty.  Please close all Firefox windows and relaunch with Autohotkey by pressing “Control-Alt-Z”
return
}

If WinActive(“ahk_id” Profile1ID)
{
WinMinimize
}
else
{
IfWinExist ahk_id %Profile1ID%
WinActivate
}
return

#x::
if !Profile2ID
{
msgbox 16, Error, Profile2ID is empty.  Please close all Firefox windows and relaunch with Autohotkey by pressing “Control-Alt-Z”
return
}
If WinActive(“ahk_id” Profile2ID)
{
WinMinimize
}
else
{
IfWinExist ahk_id %Profile2ID%
WinActivate
}
return

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.