Show a dialog with Kdialog (part 2)

Updates

The following updates have been applied:

2021-03-25:
  • Added the --imginputbox dialog
2022-01-12:
  • Added the --dontagain option paragraph
2024-01-18:
  • Added the example script for the --imginputbox dialog

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"

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"

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"

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 (equal to 0) or not (equal to 1):

#!/bin/bash
kdialog --yesno 'Having fun?' --title 'Tell the truth'
if [ $? -eq 0 ]; then
  echo 'This lesson is tasty'
else
  echo 'This lesson is boring'
fi

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'

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'

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?' ;;
esac

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'

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

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'

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 indicates the strength of the password itself.

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

Last but not least among the dialogs requiring a text, there is the imginputbox dialog. This is similar to the imgbox dialog seen in the previous article, but provides a text box below the rendered image.

$ kdialog --imginputbox "Documents/Cliparts/OpenSUSE_Logo.svg" "Which product this logo belongs to?" --title "Guess the logo"

This set of input dialogs provides the inserted string as standard output only when the Ok button has been pressed.

So an empty string might be the result of an empty input or just a Cancel button pressed.

If that makes the difference it’s important to check the exit status first:

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

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 pretty self-explanatory so let’s start with them.

When we require an existing file we choice getopenfilename

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

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

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 textbox.

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

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: pressing the Open or Save button will return zero in the exist status and one when the Cancel button is pressed:

#!/bin/bash
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"
  done
fi

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

Don’t again option

As seen in the first part, the dontagain option can be appended to some of the dialogs discussed here to limit the verbosity of our script for all those not critical messages we show.

The dialogs compatibles are those seen in the Yes or no, continue or cancel and Yes, no, cancel paragraphs.

The mechanism is the same seen previously, as a reminder here below a short example:

$ kdialog --yesno 'Do you confirm it?' --dontagain 'myapp/agreements:confirm'

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


Follow-up articles


Leave a Comment