Ceci est une ancienne révision du document !
Welcome back! In this issue, we continue our automation journey. If you have just joined us, we are automating things in Ubuntu. Last time, I asked you to save your file as test.txt. Did you manage to do it? If not, have a look at what I did.
Code:
#! /bin/ bash mousepad& sleep 1 xdotool type “I am too lazy to type this over and over again” xdotool key Ctrl+S xdotool type test.txt xdotool key KP_Enter xdotool key Alt+S
That wasn’t so difficult, was it?
What you will need: • A text editor. • xdotool on a computer running Ubuntu. • A willingness to learn.
Briefing
Before we move onto the mouse, what if the program we are opening is already open? (In my case - mousepad). What if we do not want to run a new instance of it every time our script executes? If you had a look at the man page, you would have seen “WINDOW COMMANDS”. Below that, you would have noticed the “- - onlyvisible” “Show only visible windows in the results. This means ones with map state IsViewable.” Now, if you look under the heading “DESKTOP AND WINDOW COMMANDS”, the very first entry is ‘windowactivate’ - notice how it differs from ‘windowfocus’ a few lines before that. All that is left is to name the window we are looking for, (%1 being the default). Under “WINDOWS COMMANDS”, you will find the first entry is ‘class’ - “–class Match against the window class.” The description is a bit misleading, but this is the name of the window. We will construct our command thus:
xdotool - -search - -onlyvisible - -class “mousepad&” windowactivate
replace “mousepad&” with the above line. (OR whatever you used, featherpad&, leafpad&, etc.)
Now oupen mousepad and re-run your script. Are you still with me? Did your text editor get focus and run your script? Great! Let us move forward. (We will look at text and windows in more depth later – I just want to get you excited about what you can do in a short time). If you have done something clever with what we have shown you, please go ahead and show us!).
Now let us look at moving the mouse.
If you noticed in the manpage under “MOUSE COMMANDS”, we have ‘mousemove’ and ‘mousemove_relative’ (no, you can not use your mouse to move grandma!). The option ‘mousemove’ is a fixed spot on the screen in x and y coordinates. The ‘restore’ switch will move it to the last position. You know how big your screen is, 1920×1080, or 1024×768, etc. Those are your pixel positions. To move the mouse to the top left is 0,0. Bottom right will be the screen size, like 800×600, for instance. We will use mousemove, as this is absolute positions. Clicking is another story. “Buttons generally map this way: Left mouse is 1, middle is 2, right is 3, wheel up is 4, wheel down is 5.”
Clear your code in myscript.sh, leaving only the shebang.
Try this code:
#! /bin/bash
xdotool mousemove 200, 200 sleep 1 xdotool mousemove 400, 400 sleep 1 xdotool click 3
Instead of holding your hand like the first article, your homework is to make the mouse move to your menu button, click that to start your menu, and click an entry to launch a terminal. You should have all the tools and information you need to do so now.
If you are stuck – know that your screen goes from 0,0 in the top left to 0,1920 in the top right, (if your screen size is 1920×1080) and 1080, 0 (if your screen size is 1920×1080) in the bottom left.
Troubleshooting
If you were using Geany like we used in Part One in last month’s article, you can use the terminal in the bottom pane to launch your script. If you were using a text editor, you need to launch it from the terminal. Do not forget to make your script executable. Remember to add a sleep statement in between your commands as the terminal is much faster than the GUI. Do not be afraid to go back to the previous article and do it again to refresh your memory.