paint-brush
How to Automate Your Linux Desktopby@hrishikeshpathak
13,099 reads
13,099 reads

How to Automate Your Linux Desktop

by Hrishikesh PathakAugust 15th, 2022
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

In this beginner tutorial, we will write a shell script and use X11 utilities to automate our Linux desktop. The objective of the article is to automatically set up our daily work environment with a single command. Automating a regular repetitive task can make you very productive. This is a very small tutorial to give you a starting point on automation on Linux desktops.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail

Coin Mentioned

Mention Thumbnail
featured image - How to Automate Your Linux Desktop
Hrishikesh Pathak HackerNoon profile picture


In general, automating our daily repetitive tasks save us time. But Sometimes, we programmers automate such a simple task that it took more time to automate than you manually do the actual task. I think you have seen this meme right? The funniest thing is as a programmer I can relate to this meme 😂.



Jokes aside, automating a regular repetitive task can make you very productive, if you execute it well. Automating a task can also help you to build your critical thinking ability. By doing this you will learn how to break a large problem into small chunks and solve them one by one.


What you should expect in this tutorial

In this beginner tutorial, we will write a shell script and use X11 utilities to automate our Linux desktop. The objective of the article is to automatically set up our daily work environment with a single command.


To give you an idea, when I run the script, it will open the mail client in one workspace. Then it will go to the next workspace and open Reddit and Twitter websites in the chrome browser. Thus it creates a work environment without doing all these tasks manually.


This is a very small tutorial to give you a starting point on automation on Linux desktops. You build your own automatic workflow on top of this and share it with me. I will be very glad to see them.


The final result of this tutorial looks like this.


Installing necessary tools

We are using xdotool to automate the Linux desktop. This tool is specifically built for the x11 windowing system and may not be fully compatible with Wayland.


To install the xdotool on your Linux distribution, run the following commands.


  • Debian and Ubuntu: apt-get install xdotool
  • Fedora: dnf install xdotool
  • FreeBSD: pkg install xdotool
  • OpenSUSE: zypper install xdotool

Let's automate our Linux desktop

Create a bash script and make it executable. We will use this script to run the automation.


$ touch automation.sh
$ sudo chmod +x automation.sh


Add the shebang and your bash binary path at the top of the script. This line locates the bash interpreter and executes your script.


#! /bin/bash

Programmatically open your mail client

In this example, I am using Geary as a mail client. You can use yours as well. To open Geary from the terminal, we can run the following command.


geary 1>/dev/null 2>/dev/null &


This command opens the Geary mail client and detaches the process from the terminal. Any output from the Geary app will be sent to the /dev/null location. This is essential to keep our terminal clean.


Add the above line to our bash script. It opens the Geary app when we execute the script. As opening GUI apps take time, we need to wait a little bit. Therefore add a sleep of 2 seconds after the above command.


sleep 2

Switch to the next workspace

In my desktop environment, I have to press ctrl+alt+Right in order to switch to the next workspace. Similarly, ctrl+alt+Left for the previous workspace.


Now, convert this logic into code using xdotool in our bash script. In order to navigate to the next workspace, add the following line in the script.


xdotool key ctrl+alt+Right


To avoid any possible errors, we add a sleep time of 2 seconds in the script. You can change this time as per your need.


sleep 2

Open Google-Chrome

In the second workspace, our script needs to open the google-chrome browser and open 2 websites. To open the google-chrome browser, we use the same logic as opening our mail client in the previous step. We open the google-chrome GUI and send all the terminal output to /dev/null location.


google-chrome 1>/dev/null 2>/dev/null &


Now our plan is to open 2 browser tabs. One of which contains the HackerNoon website and the other one contains the Twitter website. Think about this task. Do you know how to open a browser tab using a keyboard shortcut?


Let’s think about our usual workflow of opening a website in our web browser.


  1. We use ctrl+t to open a new browser tab.
  2. Then we put the URL on the search bar
  3. Hit enter or return to visit a webpage.


Now we have to emulate this flow using code. From the previous examples, you know how to emulate key press using xdtool right?


xdotool key ctrl+shift+t


The above command creates a new browser tab. To put your URL in the search bar, we use the type keyword of the xdotool.


xdotool type "https://www.hackernoon.com/"


Then we emulate the enter or return button to visit the website.


xdotool key Return


Now we can combine these 3 commands to run them sequentially one after another.


xdotool key "ctrl+shift+t"; xdotool type "https://www.hackernoon.com/"; xdotool key Return


Try to run this command in your terminal. Is it working? Congratulations 🎊!


Now try to write a command to open twitter.com in a new tab. You can do this. Just take a look at the previous command and slightly modify it to fit your needs.


Our automation bash script is now complete. After you write the command to open Twitter in google-chrome, the final script looks like this.


#! /bin/bash

geary </dev/null &>/dev/null &
sleep 2

#Go to workspace 2
xdotool key ctrl+alt+Right
sleep 2

google-chrome </dev/null &>/dev/null &
sleep 4

xdotool key "ctrl+t"; xdotool type "https://www.hackernoon.com/"; xdotool key Return
sleep 4

xdotool key "ctrl+t"; xdotool type "https://twitter.com/"; xdotool key Return

echo "Your workspace is ready 😎"

Running the bash script

To run our bash script, specify the script file name with the relative path to the script.


$ ./automation.sh


This will run our script in the terminal and you can see the automated app opening and workspace switching in front of you.

Conclusion

This is a beginner tutorial to learn basic Linux desktop automation. Visit the xdotool documentation for more information. If you learned something new, share it with your social peers.