Search code examples
pythonautomationpywinauto

Switch between applications - pywinauto


I am trying to loop between applications at a certain interval, for now, I came up with this just wanting to see it work, but I am not able to "focus" on the Chrome application. Note: multiple applications could be running (Chrome, Notepad, PyCharm, Skype) but I only want to switch between Notepad and Chrome So far I came up with this:

from pywinauto import application
from time import sleep
notepad = application.Application()
chrome = application.Application()

chrome.start("chrome.exe")
notepad.start("notepad.exe")


def loopApps():
    while True:
        chrome.connect(title_re="Google Chrome")
        chrome_dialog = chrome.top_window_()
        chrome_dialog.Minimize()
        print("[+] Sleeping 10 seconds")
        sleep(10)
        notepad.connect()
        notepad_dialog = notepad.top_window_()


loopApps()

I am not sure that this could be done using pyautogui as I've looked into the docs... This is not useful(and it's outdated) how to switch between two application using pywinauto 0.5.4


Solution

  • I've found the answer I was looking for here: Python win32gui set-as-foreground-window function

    You can get around the code and build your functionality