I am using the pywinauto module in python (3.7.4) on Microsoft Windows to automate a window on desktop. I need to make various functionalities to perform tasks on a window opened on the desktop like getting a window, focus on a window , maximize and minimize, close a window,move and resize window or check if the window is active or not. Following is my code
from pywinauto import application
import time
app=application.Application()
app.start('Notepad.exe')
time.sleep(5)
app1 = app.top_window()
this is the basic code to open any application and then I need to make other tasks on separate scripts as user inputs.
The following is to focus on the window ,maximize or minimize or close the window application or resize a window
app1.set_focus()
time.sleep(2)
app1.maximize()
time.sleep(2)
app1.minimize()
app1.close()
app.Notepad.move_window(0,0,1280,700)
I need to work on more functionalities but can't seem to understand what to do
As i am more familiar with pywin32, this is a way of doing above things with pywin32. These code examples assume you know the hwnd of the window you want to test
1) Window active check
test_hwnd == win32gui.GetForegroundWindow()
will give you the boolean whether or not that is the foreground window (my understanding of active)
2) List of all windows (saved to a dict {hwnd:title}
)
(modified version of How to get a list of the name of every open window?)
def get_all_windows():
windows_by_hwnd = {}
def winEnumHandler(hwnd, ctx):
title = win32gui.GetWindowText(hwnd)
if win32gui.IsWindowVisible(hwnd) and title:
windows_by_hwnd[hwnd] = title
win32gui.EnumWindows( winEnumHandler, None )
return windows_by_hwnd
3)
def move_by_offset(hwnd, x, y):
rect = win32gui.GetWindowRect(hwnd)
win32gui.MoveWindow(hwnd, rect[0]+x, rect[1]+y, rect[2]+x, rect[3]+y, True)
def move_to(hwnd, x, y):
rect = win32gui.GetWindowRect(hwnd)
win32gui.MoveWindow(hwnd, x, y, rect[2]-rect[0], rect[3]-rect[1], True)
4)
I suggest using one of the many Python command line interfaces to do the windows "open with..."
Something like
os.system("c:/path/to/exe.exe c:/path/to/file.ending")
And you are better of using subprocess here as it has better error handling than os, but there are enough answers on Stackoverflow about how to use subprocess. You will probably end up with something like subprocess.call("c:/path/to/exe.exe c:/path/to/file.ending", shell=True)
, but as i am not very familiar with subprocess, this is nothing i can say for sure is a good way of doing this.
I hope this hepls you.