{"id":26511,"date":"2022-08-31T03:00:22","date_gmt":"2022-08-31T03:00:22","guid":{"rendered":"https:\/\/sambarchi.com\/?p=26511"},"modified":"2022-08-31T03:00:23","modified_gmt":"2022-08-31T03:00:23","slug":"automation-python-copy-paste-to-the-terminal","status":"publish","type":"post","link":"https:\/\/sambarchi.com\/?p=26511","title":{"rendered":"Automation &#8211; Python &#8211; Copy\/Paste to the Terminal"},"content":{"rendered":"\n<p>Making an automation using Python via &#8220;<a href=\"https:\/\/pyautogui.readthedocs.io\/en\/latest\/index.html\">pyautogui<\/a>&#8220;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized is-style-default\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024-1024x723.jpg\" alt=\"matrix, code, computer-356024.jpg\" class=\"wp-image-26512\" width=\"838\" height=\"592\" srcset=\"https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024-1024x723.jpg 1024w, https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024-300x212.jpg 300w, https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024-768x542.jpg 768w, https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024-720x509.jpg 720w, https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/matrix-code-computer-356024.jpg 1280w\" sizes=\"(max-width: 838px) 100vw, 838px\" \/><figcaption>I wish I could tell you this is how I was thinking <br>but it took me MUCH longer than anticipated to find out how to do the simple tasks I wanted to.<\/figcaption><\/figure>\n\n\n\n<p>I&#8217;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!<\/p>\n\n\n\n<p>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&#8217;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.<\/p>\n\n\n\n<p>To begin you will want to <a href=\"https:\/\/pyautogui.readthedocs.io\/en\/latest\/install.html\">install<\/a> this library for Pyton to your computer. Since I am on a Mac this was via pip in the Terminal. <br><br>Here is this in a bit more depth.<br><br>Open your Terminal (command space and then type terminal and press enter)<br>Then type (or paste) the following and press &#8216;Enter&#8217;<br><code><code>python3\u00a0-m\u00a0pip\u00a0install\u00a0pyautogui<\/code><\/code><\/p>\n\n\n\n<p>Then you are good to start your project in your text editor of choice. Mine for today is <a href=\"https:\/\/www.sublimetext.com\/\">Sublime Text<\/a>. You can find a lot of the programs I use via my <a href=\"https:\/\/sambarchi.com\/?page_id=25403\">R\u00e9sum\u00e9<\/a> page.<\/p>\n\n\n\n<p>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:<br><code>import pyautogui<\/code><br>is what we start with<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img decoding=\"async\" loading=\"lazy\" width=\"678\" height=\"306\" src=\"https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png\" alt=\"\" class=\"wp-image-26514\" srcset=\"https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png 678w, https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM-300x135.png 300w\" sizes=\"(max-width: 678px) 100vw, 678px\" \/><figcaption>My completed project. 9 lines of sweet productivity \ud83d\ude42<\/figcaption><\/figure>\n\n\n\n<p>From here there are a few SUPER handy pyautogui commands we can use to do pretty much anything! I&#8217;ve linked documentation to pyautogui in multiple places throughout this post but here is the <a href=\"https:\/\/pyautogui.readthedocs.io\/en\/latest\/quickstart.html\">cheat sheet<\/a> of commands.<\/p>\n\n\n\n<p>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. \ud83d\ude42<\/p>\n\n\n\n<p>#This informs the system we will be using pyautogui&#8217;s syntax for this<br><code>import pyautogui<\/code><br>#This is to set up a poor mans &#8220;for&#8221; loop essentially for me I had this repeating 6 times<br><code>for x in range(6): <br>pass<\/code><br>#heres where the fun begins. the line below is instructing the computer to click a specific X (horizontal and Y (vertical) coordinate.<br><code>pyautogui.click(410,46, duration=0.5) <\/code><br>#This is telling the computer to simulate pressing the &#8220;down arrow&#8221; key.<br><code>pyautogui.press('down')<\/code><br>#This is copying to the clipboard<br><code>pyautogui.hotkey('command', 'c')<\/code><br># 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<br><code>pyautogui.click(1033,79, duration=0.5)<br>pyautogui.hotkey('command', 'v')<br>pyautogui.hotkey('enter')<\/code><\/p>\n\n\n\n<p>Top tip! The &#8220;#&#8221; 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&#8217;t be included. Nature is pretty neat like that.<\/p>\n\n\n\n<p>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!<\/p>\n\n\n\n<p>On a side note people close to me have been telling me a lot lately to &#8220;keep automation to yourself&#8221; 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&#8217;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. <\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>Happy Automating!<\/p>\n\n\n\n<p class=\"has-small-font-size\"><em>Resources I used: <br>Automate the boring stuff with Python &#8211; Chapter 20<br>Google searches for &#8220;pyautogui cheat sheet&#8221;<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making an automation using Python via &#8220;pyautogui&#8220;. I&#8217;ll be showing you these steps with a Mac. I use Sublime Text [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":26514,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_uag_custom_page_level_css":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[5,4],"tags":[],"aioseo_notices":[],"uagb_featured_image_src":{"full":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"thumbnail":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM-150x150.png",150,150,true],"medium":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM-300x135.png",300,135,true],"medium_large":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"large":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"1536x1536":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"2048x2048":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"airi-720":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"airi-360-360":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM-360x306.png",360,306,true],"airi-850-485":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false],"airi-390-280":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM-390x280.png",390,280,true],"airi-969-485":["https:\/\/sambarchi.com\/wp-content\/uploads\/2022\/08\/Screen-Shot-2022-08-30-at-10.36.21-PM.png",678,306,false]},"uagb_author_info":{"display_name":"sambarchi@gmail.com","author_link":"https:\/\/sambarchi.com\/?author=1"},"uagb_comment_info":3,"uagb_excerpt":"Making an automation using Python via &#8220;pyautogui&#8220;. I&#8217;ll be showing you these steps with a Mac. I use Sublime Text [&hellip;]","_links":{"self":[{"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/posts\/26511"}],"collection":[{"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sambarchi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26511"}],"version-history":[{"count":1,"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/posts\/26511\/revisions"}],"predecessor-version":[{"id":26515,"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/posts\/26511\/revisions\/26515"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sambarchi.com\/index.php?rest_route=\/wp\/v2\/media\/26514"}],"wp:attachment":[{"href":"https:\/\/sambarchi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sambarchi.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sambarchi.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}