If you ever needed to write shell scripts to automatize your tasks, but at the same time wanted a better integration with the running Desktop Environment, an application like Gdialog or Kdialog might be a good choice.
Being a KDE user myself, this article will focus on Kdialog, but I guess the same consideration might still be valid for Gdialog or the ncurses version dialog.
A dialog for everything
The first thing I loved since I moved my first steps into the Linux world, is the powerfull shell installed by default and a large number of command-line applications that allow writing fantastic scripts.
Kdialog is one of them and its goal is to provide a wide range of dialog’s windows, for input and output purposes: a simple message box or a file selection dialog, a mere text input dialog or a selection list. Most of the time these dialogs give substantial help providing a sort of input validation, that’s why shouldn’t be considered decoration only.
Structure of a dialog
A dialog is a window, resizable or not, with a set of elements:
- a title to introduce the dialog itself,
- a text area intended as a message output or an input request,
- a widget area , where the needed components are deployed to perform the request (for input dialogs only),
- a set of buttons at the bottom to complete the action.
Later we will see how, reading the standard output and the exit status after a Kdialog call, we will have full control over the choice made.
Let’s start with the output dialogs.
The simplest use case is a message box with an optional title:
$ kdialog --msgbox "Hello World!" --title "Just saying hello"
The icon in the dialog shows that the kind of message is purely informative and doesn’t deserve particular attention, it can be used to communicate the end of a process or the result of a calculation.
The –title switch is optional and can be used for the whole set of dialogs provided by Kdialog.
We can also warn the user:
$ kdialog --sorry "Ehi, I am here!" --title "Warning you"
Or communicate an error:
$ kdialog --error "You made a mistake" --title "Something went wrong"
Giving more details
In the latest two cases, we can provide more info within a hidden textbox, which can be displayed pressing the Details button.
$ kdialog --detailedsorry "Ehi, I am here" "Behave or I'll tell your mom!" --title "Warning you"
And the detailederror:
$ kdialog --detailederror "You made a mistake" "Didn't consider the other opportunity" --title "Something went wrong"
Another less known output dialog is represented by the Passive Popup which is just a notification message showing above the bottom toolbar which disappears after nth seconds and can show an optional icon:
$ kdialog --passivepopup "I am going to vanish in three seconds" 3 --icon dialog-error --title "Auto-destructive message"
The icon parameter accepts one of the icon names included in the current theme, it defaults to dialog-information, the same icon used by the msgbox dialog. Later we will see how to get the list without dig into the icon theme folders, but just using Kdialog.
Reading from a file
A useful output dialog is the textbox: it’s intended to show the content of a text file within a resizable window having only a text area and an ok button.
$ kdialog --textbox ~/.bashrc --geometry 640x400+400+300 --title ".bashrc content"
Being the window resizable we can use the optional geometry switch to fix, respectively, the width, height, and the offset from the left and the top border of the screen.
Last but not least the progressbar is the most complex among the output dialogs:
$ kdialog --progressbar "I am progressing" 10 --title "Step by step" --geometry 400x200+400+300
Once the dialog is shown, something is printed and the console is left free to evaluate other commands.
To interact with that dialog we need the power of qdbus and that printed code:
$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'value' 5
We can also set the autoclose when the value reaches the max
$ qdbus org.kde.kdialog-5433 /ProgressDialog org.freedesktop.DBus.Properties.Set '' 'autoClose' 1
In this first part, I have shown the whole set of Kdialog’s output dialogs, next we will see how to handle the input dialogs and how to use them within our scripts or inside a terminal.