Show a dialog with Kdialog (part 2)

In a previous article I introduced Kdialog and the whole set of output dialogs provided. In this second part, we will see how to show and handle some input dialogs from the command line.

Get inputs

CLI applications in general, and Bash scripts in particular, may get the user input that way:

  1. As parameter when calling the script;
  2. from the standard input STDIN;
  3. using the read instruction inside the script.

Let’s think to Kdialog as a read command with steroids, with it you can ask everything and restrict your request to what your script needs to know: a simple yes/no answer, a filename, a directory, a string, a text, a date, a number, a list of items or an item among a list of values.

Yes or no, continue or cancel

The simplest answer we can get it’s in the boolean domain: yes/no, true/false, ok/cancel, and the yesno dialog fits perfectly that purpose:

$ kdialog --yesno "Do you confirm it?" --yes-label "Confirm" --no-label "Reject" --title "Confirm or not"

kdialog --yesno

kdialog --yesno

The optional switches –yes-label and –no-label allow overwriting the yes button and no button labels.

A variation of that dialog is the warningyesno dialog:

$ kdialog --warningyesno "Do you confirm it?" --yes-label "Confirm" --no-label "Reject" --title "Be clear please"

kdialog --warningyesno

kdialog --warningyesno

The only difference is the icon shown.

A dialog similar to the last one is the warningcontinuecancel, which has the continue button in place of the yes button and the cancel button replacing the no button:

$ kdialog --warningcontinuecancel "Do you want to continue?" --continue-label "Let's go" --cancel-label "No way" --title "Continue or not"

kdialog --warningcontinuecancel

kdialog --warningcontinuecancel

How to handle the answers from these dialogs is quite easy, just reading the exit status in the $? variable we may know if the answer is positive ($? value is equal to 0) or not ($? value is equal to 1):

kdialog --yesno 'Having fun?' --title 'Tell the truth'
if [ $? -eq 0 ]; then
  echo 'This lesson is tasty'
  echo 'This lesson is boring'

Yes, no, cancel

Sometimes we want the opportunity to skip a binary question, that’s why we need a third button_

$ kdialog --yesnocancel 'Do you like this tutorial?' --cancel-label 'Skip' --title 'Answer or skip'

kdialog --yesnocancel

kdialog --yesnocancel

A variation is the warningyesnocancel dialog which shows a warning icon:

$ kdialog --warningyesnocancel 'Do you like this tutorial?' --cancel-label 'Skip' --title 'Think twice before answer'

kdialog --warningyesnocancel

kdialog --warningyesnocancel

The logic behind the result is the same, but when the third button is pressed Kdialog will return the value 2:

kdialog --yesnocancel 'Is the glass half full?' --cancel-label 'I dunno' --title 'Look at it'
case $? in
0) echo 'You are an optimist' ;;
1) echo 'Is not as bad as you think' ;;
2) echo 'No glasses around?' ;;

Write something

The next set of input dialogs is focused on text input, the simplest interface to call is inputbox:

$ kdialog --inputbox 'Describe your day in few words' 'Default text' --title 'Use your words'

kdialog --inputbox

kdialog --inputbox

The Default text is optional, and using an empty quoted text '' before the next option, will let it empty.

To get a larger text better use the textinputbox dialog:

$ kdialog --textinputbox 'Write your poeme' '' --title 'Unleash your creativity' --geometry 600x400+400+250

kdialog --textinputbox

kdialog --textinputbox

With this dialog, we can optionally use –geometry to resize the window.

To request a string to keep hidden from prying eyes, the password dialog comes in help:

$ kdialog --password 'Write your password' --title 'Tell me a secret'

kdialog --password

kdialog --password

The password string is indeed replaced by black points.

The best solution to request a new password is instead the newpassword dialog, which accepts a string if and only correctly repeated in a second textbox, a progress bar calculate the strength of the password itself.

$ kdialog --newpassword 'Write your new password' --title 'Tell me a secret'

kdialog --newpassword

kdialog --newpassword

This set of input dialogs provide the inserted string as standard output only after the Ok button has been pressed. So an empty string might result from an empty input or just a Cancel button, if that makes difference is important to check for the exit status:

answer=`kdialog --inputbox 'Which color I am thinking about?' --title 'Guess it'`
if [ -z "$answer" ]; then
  if [ $? -eq 0 ]; then
    kdialog --sorry 'Empty answer'
    kdialog --msgbox 'Question canceled'
elif [ "$answer" = 'blue' ]; then
  kdialog --msgbox 'Congratulations, you read my mind!'
  echo 'You lost!'

Handling files and directories

To handle files and directories there are five dialogs, but a couple of them looks perfectly equivalent to another couple, except for the name:

  • getopenfilename / getopenurl
  • getsavefilename / getsaveurl
  • getexistingdirectory

Their name is, as always, pretty explanatory so let’s start to examine them.

When we require an existing file we choice getopenfilename

$ kdialog --getopenfilename ~/bin 'Png and Ruby files (*.png *.rb)'

kdialog --getopenfilename

kdialog --getopenfilename

The optional parameters are:

  • the starting directory, where read the files,
  • the files filter which can be specified as seen above or using one or more space-separated mime types:
    $ kdialog --getopenfilename ~/bin 'image/png application/x-ruby'

Another option is –multiple which allows selecting several files at once, it’s always a good idea to use together with –separate-output to have each file name in a separate line.

$ kdialog --getopenfilename ~/bin 'image/png application/x-ruby' --multiple --separate-output

The getopenfilename dialog doesn’t allow to select either directories or not existing files, trying to do it through the input box will raise a warning message (a sorry dialog).

To select a directory instead, you have to use the getexistingdirectory dialog:

$ kdialog --getexistingdirectory ~/bin

kdialog --getexistingdirectory

kdialog --getexistingdirectory

It allows us to create also a folder using the related button in the dialog, multiple items are not allowed.

The last dialog about file handling is getsavefilename, it looks like the getopenfilename dialog and the only difference is the ability to accept not-existing files writing the name directly in the text box.

$ kdialog --getsavefilename ~/bin 'Png and Ruby files (*.png *.rb)'

kdialog --getsavefilename

kdialog --getsavefilename

An issue i noticed with getsavefilename is in the file type filter: specifying the mime types only the first will be considered, the custom text, as shown above, it’s a good workaround.

Using these dialogs is not much different than the previous ones, this time pressing the Open or Save button will return zero in the exist status and one when the cancel button is pressed:

files=`kdialog --getopenfilename ~/Documents --multiple --separate-output`
if [ $? -eq 0 ]; then
  echo '* Printing mime types for each file:'
  for i in $files do;
    file --mime-type "$i"

The example above just prints the mime types for each file selected using the file command.

Final thoughts

This second part is ended up, but the list of the input box is not finished yet, that’s why in a third article we will see the input based on list values and miscellanea types of dialogs.


Creative Commons License This work is licensed under a Creative Commons Attribution-NoCommercial-ShareAlike 4.0 International License

Share to

Related articles