Python PyAutoGUI useful scripts

PyAutoGUI Activate Window and Bring Window to Front:

import pygetwindow as gw
win = gw.getWindowsWithTitle('Your Window Title')[0]
win.activate()

You can use the pygetwindow module to get the titles of all currently active windows. Here is a Python script that does this:

import pygetwindow as gw

# Get list of all windows
all_windows = gw.getAllWindows()

# Iterate through the list and print the window titles
for window in all_windows:
print(window.title)

This script will print the title of each window that is currently open on your computer. Note that the window title is the text that is displayed in the title bar of the window.

Before running the script, make sure to install the pygetwindow module if you haven't already done so. You can install it with the following command:

pip install pygetwindow

Once you have the titles, you can use the title you want in the 'Your Window Title' placeholder in the previous scripts.

PyAutoGUI Scroll to Bottom of Page:

import pyautogui
pyautogui.scroll(-1000)

PyAutoGUI Click and Drag:

import pyautogui
pyautogui.dragTo(100, 200, button='left')

PyAutoGUI Check if Key is Pressed:

import keyboard  # using keyboard module as PyAutoGUI does not support this
if keyboard.is_pressed('a'): # if key 'a' is pressed
print('You Pressed A Key!')

Convert PyAutoGUI Image to OpenCV:

import pyautogui
import cv2
import numpy as np

screenshot = pyautogui.screenshot()
opencv_image = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)

PyAutoGUI Detect Mouse Click (PyAutoGUI itself doesn't provide this, but you can use pynput library):

from pynput.mouse import Listener

def on_click(x, y, button, pressed):
print('{0} at {1}'.format('Pressed' if pressed else 'Released', (x, y)))

with Listener(on_click=on_click) as listener:
listener.join()

PyAutoGUI Display Mouse Position:

import pyautogui
print(pyautogui.position())

PyAutoGUI Get Mouse Position on Click (Use pynput library):

from pynput.mouse import Listener

def on_click(x, y, button, pressed):
if pressed:
print('Mouse clicked at ({0}, {1})'.format(x, y))

with Listener(on_click=on_click) as listener:
listener.join()

PyAutoGUI Highlight Text (Depends on application. Here is a generic example):

import pyautogui
pyautogui.moveTo(100, 150)
pyautogui.dragTo(400, 150, button='left')

PyAutoGUI Hold Right Click:

import pyautogui
pyautogui.mouseDown(button='right') # Hold down the right mouse button

PyAutoGUI Hold Key for Time:

import pyautogui
import time

pyautogui.keyDown('shift')
time.sleep(1) # Hold for 1 second
pyautogui.keyUp('shift')

How to Take a Screenshot with PyAutoGUI:

import pyautogui
im = pyautogui.screenshot()
im.save('my_screenshot.png')

PyAutoGUI Keep Awake:

import pyautogui
import time

while True:
pyautogui.move(1, 0) # Move mouse cursor one pixel to the right
time.sleep(60) # Wait for 60 seconds

PyAutoGUI Key Combination:

import pyautogui
pyautogui.hotkey('ctrl', 'c') # Presses the Ctrl+C hotkey combination

Open Application using PyAutoGUI (You can use os module):

import os
os.startfile(r'C:\path\to\your\application.exe')

PyAutoGUI Take Screenshot of Specific Window:

import pyautogui
import pygetwindow as gw

win = gw.getWindowsWithTitle('Your Window Title')[0]
screenshot = pyautogui.screenshot(region=(
win.left, win.top, win.width, win.height))
screenshot.save('my_screenshot.png')

PyAutoGUI with Multiple Monitors:

import pyautogui

# Get screen size for all monitors
screen_info = pyautogui.info()
print(screen_info)

Please replace 'Your Window Title' with the title of the window you want to manipulate. Also, make sure to handle any exceptions that could be raised when the desired window is not found. And don't forget to install required packages such as keyboard, pynput or pygetwindow if they are not installed yet.

In our exploration of Python's PyAutoGUI, we encounter a variety of scripts that can aid in automating certain tasks. One such task is controlling mouse movement, a foundational aspect of many automation processes. If you are in need of understanding how to handle mouse actions using your scripts, it could be beneficial to learn how to move your mouse with PyAutoGUI. This guide offers detailed instructions and examples, presenting a pragmatic approach to handling mouse control in your Python automation scripts using PyAutoGUI.

Published