Automation with PyAutoGUI in Python

Pyautogui Poster Min

Let’s learn to automate tasks with PyAutoGUI in Python. Whenever we come across a task that requires repetition, we try to come up with methods specifically to avoid it. That’s human nature.

Somewhere along the line of working hard on the same task, we’ve come across an idea that we can create something that functions automatically and only needs to meet a set number of conditions to work.

Be it a lawnmower, which requires batteries, and a field of grass, or be it code that prints the same line again and again.

Automating has become a huge part of our lives as humans, and working with automation allows for us to focus on other tasks while the process takes place.

However, automation requires tools to work with, and that’s where pyautogui module comes into the picture.

The pyautogui module allows for the running script to control your mouse and keyboard, providing input much like how a user on the system would, allowing for interactions between applications on the system.

Install PyAutoGUI in Python

We can install PyAutoGUI in Python via the PIP package manager. You can use the same lines for installing on any operating system that works with pip.

# Windows does not have any dependencies for installation
pip install pyautogui

# Mac has two dependencies for PyAutoGUI
pip3 install pyobjc-core
pip3 install pyobjc
pip3 install pyautogui

# Linux distributions require a single dependency installed
pip3 install python3-xlib
pip3 install pyautogui

Once we have the dependencies (if any) and the module installed, we’re good to go!

Working with Python PyAutoGUI

Before working with all the great functions provided by the PyAutoGUI in Python, we must first import the module in the script.

# Importing the PyAutoGUI module
import pyautogui as pag

We will be using an alias for the pyautogui module throughout this article, which we have termed above as pag.

1. PyAutoGUI Basic Functions

Before working on any script, it’s better for us to know which components perform what kind of task.

That being said, pyautogui in Python provides a good variety of methods to work with input,

# Gets the size of the primary monitor.
screenWidth, screenHeight = pag.size() 

# Gets the XY position of the mouse.
currentMouseX, currentMouseY = pag.position() 

# Move the mouse to XY coordinates.
pag.moveTo(100, 150)

# Allows the script to click with the mouse.
pag.click()

# Move the mouse to XY coordinates and click it.
pag.click(100, 200)

# Find where button.png appears on the screen and click it.
pag.click('button.png') 

# Double clicks the mouse.
pag.doubleClick()

# The writing functionality provided by PyAutoGUI imitates keyboard input
pag.write('Hello world!')

# Presses the Esc key.
pag.press('esc')

# The keyDown button causes the script to hold down on a specific key.
pag.keyDown('shift')

# You can pass a list of keys to press, which will be consecutively executed.
pag.press(['left', 'left', 'left', 'left'])

# Lets go of a certain key.
pag.keyUp('shift')

 # The hotkey() function allows for a selection of keys for hotkey usage.
pag.hotkey('ctrl', 'c')

# Make an alert box appear and pause the program until OK is clicked.
pag.alert('This is the message to display.')

It’s also an important thing to note that the module also provides keywords to work in the script, which can be accessed by pyautogui.KEY_NAMES.

2. Simple Automation using PyAutoGUI in Python

We can create a simple spam automation to continuously send messages on any platform using a bit of Python, and the pyautogui module.

Let’s first import a few modules to work with the required functions.

# Importing the pyautogui module
import pyautogui as pag

# Importing time to delay the input speed
import time

# Working with Tkinter allows us to use a GUI interface to select the file to read from
from tkinter import Tk
from tkinter.filedialog import askopenfilename

Now, here’s how you get to making a spam bot.

2.1. Provide a method of input.

We can provide input by manually typing the message, but, that would defeat the purpose of even automating the message spamming.

So, let’s work with files to parse a file and write the contents to the platform. We will be using the tkinter module to select the file to read from.

# The withdraw function hides the root window of Tkinter
Tk().withdraw()

# The askopenfilename() takes the file path from user selection.
filename = askopenfilename()

Now, we have the path of the file through the askopenfilename() function. This path is stored in the filename variable.

2.2. Create a delay adjusting the speed of the spam.

We’ll need to also create a delay between each message so that the platform is able to accept messages one by one rather than a single message overwriting itself due to platform input lag.

# We take the input of the user and strip it such that we only receive a numeric input.
timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()[0])

# In case the input time is designed to break the delay function, we can reset the timeDelay back to 1.
if timeDelay < 1:
    timeDelay = 1

# We need to place the cursor in the right place to begin writing to the platform.
time.sleep(5)

2.3. Spam using PyAutoGUI!

We can now use the pyautogui module to read every word from the file, and write to the platform.

f = open(filename, "r")
for word in f:
    time.sleep(timeDelay)
    pag.typewrite(word)
    pag.press("enter")

3. Complete Implementation of PyAutogui in Python

We are now done with the code, your final code should look something like this,

import pyautogui as pag
import time
from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw()
filename = askopenfilename()
print(filename)

timeDelay = int(input("If you want a delay, enter the number of seconds for the delay : ").split()[0])

if timeDelay < 1:
    timeDelay = 1

time.sleep(5)

f = open(filename, "r")
for word in f:
    time.sleep(timeDelay)
    pag.typewrite(word)
    pag.press("enter")

Conclusion

Now that you’re done with this article, you know what pyautogui in Python offers, and what you can use it for.

While we wouldn’t necessarily recommend spamming, tinkering about is completely acceptable 😉

Check out our other articles, Working with the Pandas module, Numpy Arrays, and Creating a Hi-Lo game using Pygame.

References