Automating Desktop Applications with Python's PyAutoGUI Library

Getting Started

Open PowerShell or Terminal on your local machine, activate your Python environment, and install PyAutoGUI using the following command:

pip install pyautogui

Once installed, you can begin using the library by importing it:

import pyautogui

Core Functions

1. Screen and Mouse Position

The coordinate system originates from the top-left corner of the screen. You can retrieve the screen dimensions and current mouse coordinates with these functions:

# Get screen resolution (primary monitor only)
screen_width, screen_height = pyautogui.size()

# Get current mouse coordinates
current_x, current_y = pyautogui.position()

2. Mouse Movement

Use moveTo() for absolute movement or moveRel() for relative movement from the current position:

# Move to absolute coordinates over 1 second
target_x, target_y = 409, 300
pyautogui.moveTo(target_x, target_y, duration=1.0)

# Move relative to current position
delta_x, delta_y = 50, -30
pyautogui.moveRel(delta_x, delta_y, duration=0.5)

3. Mouse Clicking

The click() method performs mouse clicks with various parameters:

# Click at specified coordinates
click_x, click_y = 620, 538
pyautogui.click(x=click_x, y=click_y, clicks=2, interval=1.0, button='left')

Practical Example: Batch Unfollowing WeChat Official Accounts

When managing hundreds of followed accounts, manual unfollowing becomes tedious. This automation script handles the process efficiently.

Manual Process Analysis

The manual unfolllowing procedure involves:

  1. Navigating to Contacts → Official Accounts (manual step)
  2. Cilcking target account → "View History Messages"
  3. Clicking "Followed" → "Unfollow"

Coordinate Identification

Using pyautogui.position() while hoveirng over elements reveals their screen coordinates. For dynamic elements, use image recognition instead.

Image Recognition for Dynamic Elements

The locateOnScreen() function finds UI elements by matching screenshot templates:

# Locate and click "View History Messages" button
history_btn = pyautogui.locateOnScreen('history_button.png', confidence=0.7)
if history_btn:
    center_x = history_btn.left / 2 + 15
    center_y = history_btn.top / 2 + 4
    pyautogui.click(center_x, center_y)

Complete Automation Script

import pyautogui
import time

def click_at(x, y):
    pyautogui.click(x=x, y=y, clicks=1, interval=1.0, button='left')

for i in range(10):  # Process 10 accounts
    try:
        print(f"Unfollowing account {i+1}")
        
        # Click target account
        click_at(509, 497)
        
        # Click "View History Messages" using image recognition
        history_location = pyautogui.locateOnScreen('history_icon.png', confidence=0.7)
        if history_location:
            click_at(history_location.left/2 + 15, history_location.top/2 + 4)
            time.sleep(1.5)  # Wait for page load
        
        # Click "Followed" button
        follow_location = pyautogui.locateOnScreen('followed_icon.png', confidence=0.7)
        if follow_location:
            click_at(follow_location.left/2 + 25, follow_location.top/2 + 10)
        
        # Click "Unfollow"
        click_at(949, 620)
        
    except Exception as error:
        print(f"Error processing account: {error}")
        continue

Tags: pyautogui python Desktop Automation Image Recognition Mouse Control

Posted on Tue, 21 Jul 2026 16:36:43 +0000 by maltech