Automation – Python – Copy/Paste to the Terminal

Making an automation using Python via “pyautogui“.

matrix, code, computer-356024.jpg
I wish I could tell you this is how I was thinking
but it took me MUCH longer than anticipated to find out how to do the simple tasks I wanted to.

I’ll be showing you these steps with a Mac. I use Sublime Text for text editing my documents and am still learning these processes day by day. If you have any advice I am certainly open to this!

Pyautogui allows you to control your mouse and keyboard with a single monitor to do tasks. It felt the most natural to me at the start of my Automation Journey since in a sense it’s basically telling the computer step by step to click in certain areas, then use certain keyboard shortcuts. Almost like I was teaching someone who had never used a computer what to do.

To begin you will want to install this library for Pyton to your computer. Since I am on a Mac this was via pip in the Terminal.

Here is this in a bit more depth.

Open your Terminal (command space and then type terminal and press enter)
Then type (or paste) the following and press ‘Enter’
python3 -m pip install pyautogui

Then you are good to start your project in your text editor of choice. Mine for today is Sublime Text. You can find a lot of the programs I use via my Résumé page.

From here the first step is to let the computer know what library to look in. Basically it will inform the system what commands are going to be valid during the programs duration. Thus:
import pyautogui
is what we start with

My completed project. 9 lines of sweet productivity 🙂

From here there are a few SUPER handy pyautogui commands we can use to do pretty much anything! I’ve linked documentation to pyautogui in multiple places throughout this post but here is the cheat sheet of commands.

Here is the full code I used for what I needed in text form along with a breakdown of each line and what it is doing in case you wish to use it as a starting point. I hope you find it helpful. 🙂

#This informs the system we will be using pyautogui’s syntax for this
import pyautogui
#This is to set up a poor mans “for” loop essentially for me I had this repeating 6 times
for x in range(6):
pass

#heres where the fun begins. the line below is instructing the computer to click a specific X (horizontal and Y (vertical) coordinate.
pyautogui.click(410,46, duration=0.5)
#This is telling the computer to simulate pressing the “down arrow” key.
pyautogui.press('down')
#This is copying to the clipboard
pyautogui.hotkey('command', 'c')
# Finally the last 3 commands 1. moves to another location and then 2. pastes the clipboard into the Terminal and 3. submits the text with the enter button
pyautogui.click(1033,79, duration=0.5)
pyautogui.hotkey('command', 'v')
pyautogui.hotkey('enter')

Top tip! The “#” lets you make notes within your text document in case others view your projects after you have moved on to greener pastures. So if you were to copy and paste my whole code example the extra explanation wouldn’t be included. Nature is pretty neat like that.

The only downside on this is the Api I was accessing with this no longer worked properly so I am still talking to support on this one and learning as much as I can to make this happen. I will get there though and in the meanwhile I have been working on an automation for creating directories (folders). Looking forward to sharing as I find those and more!

On a side note people close to me have been telling me a lot lately to “keep automation to yourself” while at work. In a way I understand the fear, no one wants to be replaced and least of all someone who is making the automations. At the same time I don’t want to lie about how I spend my time and I will still be needed if these things changed slightly or the data was needed to be pulled from elsewhere. The human element is still valuable for today thankfully.

My intent is to automate the parts of my job that I dislike (the repetitive parts) and use the free time to be more prepared for last moment projects and other UX/UI opportunities that come my way within the company.

Happy Automating!

Resources I used:
Automate the boring stuff with Python – Chapter 20
Google searches for “pyautogui cheat sheet”

Leave a Reply

Your email address will not be published. Required fields are marked *