Switching to a specific application window using python, Various sources and custom edits.
import win32gui toplist = [] winlist = [] def enum_callback(hwnd, results): winlist.append((hwnd, win32gui.GetWindowText(hwnd))) win32gui.EnumWindows(enum_callback, toplist) browser = [(hwnd, title) for hwnd, title in winlist if 'chrome' in title.lower()] # just grab the first window that matches browser = browser[0] # use the window handle to set focus win32gui.SetForegroundWindow(browser[0]) #to minimize import win32con win32gui.ShowWindow(browser[0], win32con.SW_MINIMIZE) Using a PID #run in main application (houdini in my case) import os os.environ.update({"HOUDINIPID":str(os.getpid())}) #launch external tool through any method you wish #Use code below to switch back to your main app etc import win32con import win32gui import win32process def get_hwnds_for_pid (pid): def callback (hwnd, hwnds): if win32gui.IsWindowVisible (hwnd) and win32gui.IsWindowEnabled(hwnd): _, found_pid = win32process.GetWindowThreadProcessId(hwnd) if found_pid == pid: hwnds.append (hwnd) return True hwnds = [] win32gui.EnumWindows (callback, hwnds) return hwnds for i in get_hwnds_for_pid(int(os.getenv("HOUDINIPID"))): win32gui.SetForegroundWindow(i)