Trying to condition off of frontmost window

If you have a question or need help, this is the place to be.
Post Reply
Posts: 3
Joined: Tue Dec 03, 2013 8:34 pm

Trying to condition off of frontmost window

Post by jryan15 » Sat Sep 12, 2020 4:33 am

Hi there,

I'm trying to create a toggle that switches between two applications depending on which is the front window.

For example:
if app1 is in front launch app2
Else if app2 is in front launch app2

I've been trying to use the find window command with the "only match frontmost window" box checked, but it doesn't seem to let me select an application to match it to. I suspect it is meant to only Target the frontmost window.

However I can't figure out another method to determine which window is in front and condition off of it. I've searched allover the net and can't figure this one out.

Btw I've updated to the latest stable version 1722 with a windows 7 pc.


User avatar
Plugin Developer
Posts: 730
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: Trying to condition off of frontmost window

Post by Sem;colon » Sat Sep 12, 2020 7:53 am

Hi jryan15,

The thing you need is the plugin "task monitor".
Here you will get an event whenever a window is focused.

Here how to use it:
Create 4 macros:
App 1 selected
App 2 selected
Focus App 1
Focus App 2

When your window from app1 is focused, you will get an event. On that event you trigger the macro "app 1 selected". Inside the macro you perform two actions: "Disable macro Focus App 1" and "Enable macro Focus App 2".
Do the same with "app 2 selected" but for the other app.
Now, in the "focus app" macros set the same event trigger (button press) on both and as action let them focus app 1 in "focus app 1" and 2 in"focus app 2"

Tada! :)
If you like my work, Image me a drink :wink:

User avatar
Site Admin
Posts: 5498
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Trying to condition off of frontmost window

Post by kgschlosser » Sat Sep 12, 2020 8:01 am

you need to know what the zorder is to determine which application is in front of another.

This script enumerate all of the top level windows. the list of window handles returned is the same order as the ZOrder. the first handle returned is the top most and the last is the bottom.

I then get the process name for the window. This is what I am using to compare to. You will need to change the PROCESS_NAME_1 and PROCESS_NAME_2 variables to contain the process names for the applications you are dealing with.

If a matching process name is found it will trigger an event that is like "TopWindow.Some Window Title". If both applications are running the topmost one is going to trigger an event. You can base what you want to do off that event.

This is pseudo code and has not been tested, if you have a problem with it let me know.

Code: Select all

from eg.WinApi import GetTopLevelWindowList
from eg.WinApi.Utils import GetWindowProcessName
import ctypes
from ctypes.wintypes import HWND, INT

PROCESS_NAME_1 = 'some_process_1.exe'
PROCESS_NAME_2 = 'some_process_2.exe'

user32 = ctypes.windll.User32

_GetWindowTextLengthW = user32.GetWindowTextLengthW
_GetWindowTextLengthW.restype = INT

_GetWindowTextW = user32.GetWindowTextW
_GetWindowTextW.restype = INT

def get_window_text(handle):
    text_len = _GetWindowTextLengthW(HWND(handle))
    buf = ctypes.create_unicode_buffer(text_len.value)

    _GetWindowTextW(HWND(handle), buf, text_len.value + 1)

    return buf.value

for hwnd in GetTopLevelWindowList(True):
    process_name = GetWindowProcessName(hwnd)
    if process_name in (PROCESS_NAME_1, PROCESS_NAME_2):
        suffix = get_window_text(hwnd)
        eg.TriggerEvent(prefix='TopWindow', suffix=suffix, payload=[process_name])
If you like the work I have been doing then feel free to Image

Posts: 3
Joined: Tue Dec 03, 2013 8:34 pm

Re: Trying to condition off of frontmost window

Post by jryan15 » Sun Sep 13, 2020 5:13 pm

Thanks for the help all! I'm still a little weak at scripting so I went with the create/destroy proceed method and it works great!

Post Reply