3

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.

1
  • 1
    Generally, no. GUI applications are typically 1) compiled executables and 2) GUI buttons are connected to their specific function in the source code of application itself. In some cases, such as settings of sound, wallpaper - for those exist client software like gsettings which can be used in scripts and does almost same thing as GUI app. You can also use xdotool to simulate keyboard press and send it to GUI window. But otherwise no, there's no way to control buttons of a program – Sergiy Kolodyazhnyy Oct 4 '18 at 19:14
3

To control a GUI application from terminal commands, you may use tools like xdotool and xsel.

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.

xsel or 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:

SELECTION=$(xsel -b)

Some remarks:

  • 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.
0

A very simple shell script with GUI that:

  1. Checks the version of Firefox.
  2. If Firefox version not found it will be assumed that is not installed, will throw a message how to install it.
  3. 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:

bash ~/Desktop/yourfile
5
  • my question is regarding how to control an application from the terminal after launching it. For example, how would you browse through firefox without using the GUI provided and just typing commands in the terminal. – Rishabh Jain Oct 4 '18 at 22:21
  • Are you talking about hacking? – GoldHaloWings Oct 4 '18 at 22:43
  • No, I just want to execute certain commands in an application depending on some conditions. I know the logic for it. If I can find out the function which the application executes internally and can give it through the terminal, then I don't have to spend time waiting. – Rishabh Jain Oct 4 '18 at 23:04
  • According to: "I need to know the internal functions the application is executing when I click on a button or set some parameter" see from shell script: the var response for e.x when is 0 it executes Firefox and when is 1 it quits, this script is an example to get the idea – GoldHaloWings Oct 4 '18 at 23:05
  • I know that i am look stupid in my answers to you, but the reason is you din't specify more details are you talking for a specific software or all software in general? – GoldHaloWings Oct 4 '18 at 23:26
0

Something like Selenium could work if you just want to control something like Firefox, (also the command Firefox https://www.google.com works) For example: (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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.