I have some sequence of actions that I perform using an application provided by a third party. I want to write a bash script such that the whole thing can be executed from the terminal. But for that, I need to know the internal functions the application is executing when I click on a button or set some parameter. Also, is it even possible to control a GUI using terminal commands only? Any help would be highly appreciated.
To control a GUI application from terminal commands, you may use tools like
xdotool simulates key presses and mouse actions. You can thus write a script that will send keys and mouse clicks in sequence to the application, which will interact on these just as if you would have been using the keyboard and mouse yourself.
xclip are tools allowing you to retrieve or set the clipboard from the command line.
Just as an illustration and give a very first idea on how it works, following command would select a word before the cursor and cut it to the clipboard.
xdotool key ctrl+shift+Left ctrl+x
Your script could then retrieve the clipboard selection to do something with it:
- These tools will work reliably only on X, not, or less reiably, on the new display server Wayland
- You will need to include sufficient delays. The system may discard keystrokes if provided too fast. It takes time tuning a script in this respect. Yet, if you succeed, you will be able to repeat a same task significantly faster and without errors.
A very simple shell script with GUI that:
- Checks the version of Firefox.
- If Firefox version not found it will be assumed that is not installed, will throw a message how to install it.
- Firefox can be launched only if you click OK.
You can change it according to your needs:
#!/bin/bash clear; firefox --version >/dev/null 2>&1; check=$? if [ "$check" != "0" ]; then echo "Firefox not found!" zenity --width=300 --info --text=" How to install: sudo apt-get install firefox" 2> /dev/null exit 1 elif [ "$check" = "0" ]; then echo "Found:" firefox --version; anser=$(zenity --title="Firefox Launcher" --info --text "Launch Firefox?" --ok-label="OK" --width=150 2> /dev/null) response=$? fi if [ "$response" = "0" ]; then echo "Launching Firefox..." firefox 2> /dev/null; echo "Firefox has been launched" elif [ "$response" = "1" ]; then echo "Quit" fi
Copy the above code to a text editor and name the file anything you like, save it in your Desktop directory (or anywhere you prefer)
I will assume that the script file is in your Desktop directory.
To execute the script, launch the terminal and type:
Something like Selenium could work if you just want to control something like Firefox, (also the command
Firefox https://www.google.com works)
(This is Python 3 but something simillar fould would work on java)
from selenium import webdriver #import the webdriver object browser = webdriver.Firefox() # open a Firefox window, this also requires a firefox driver (geckodriver) browser.get("https://www.google.com") searchbar = browser.find_element_by_css_selector("input") searchbar.send_keys("example google search")
If you want to run some you want to run something else though, then try looking into something like x-runner.
If you're looking for windows that would be winrunner