0

I am working on a code that uses pyautogui and the only thing I need is to switch to a already openned application other than alt + tab to it, for example:

import pyautogui

def start():

    #here comes the code that swap to the already oppened app, Paint.exe for example
    
    pyautogui.click(x, y) #click on a tool
    pyautogui.click(x, y) #select a colour
    pyautogui.click(x, y) #paint the background

This is only a example, my code is a lot more complex and I am using pyautogui.hotkey("alt", "tab") as a solution to that, but the app always need to be the last one used, otherwise it wont switch to it, so that results in error sometimes.

Does anyone know a solution to this?

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
Chernobot
  • 1
  • 1
  • on Linux you could use bash commands like `wmctrl` to switch window by its title. – furas Sep 16 '22 at 22:08
  • it seems `pyautogui` may have undocumented function `getWindowsWithTitle()`. Maybe it has some option to focus this window (show it as top window). But this is very old information - see [Python pyautogui window handle - Stack Overflow](https://stackoverflow.com/questions/43785927/python-pyautogui-window-handle) – furas Sep 16 '22 at 22:11
  • [Python Window Activation - Stack Overflow](https://stackoverflow.com/questions/2090464/python-window-activation) – furas Sep 16 '22 at 22:16

1 Answers1

0

i'm doing it with a combination of pyautogui and ctypes. it's a mish-mash of different approaches that i tried, but couldnt get to work individually. i'm new to this kind of programming, and it's probably possible to do it with just one or the other. but it served my purposes, and i see you havent gotten any better responses, so... something like this:

windows=pyautogui.getAllWindows() 
hWnd = [x._hWnd for x in windows if x.title=='title of window to open']
user32 = ctypes.windll.user32
user32.SetForegroundWindow(hWnd)
if user32.IsIconic(hWnd): user32.ShowWindow(hWnd, 9)            
ericwerfel
  • 29
  • 7