Zenity create file selection dialog

Share this post to your friends !

This entry is part 6 of 13 in the series GUI for bash script using Zenity

WriteBash - To create a file selection dialog with zenity, we use the --file-selection option. This is a very useful option. It allows you to create a program that allows you to select files to open or read files. In fact, you have seen many programs that allow you to select and open a file. Through this article, you can understand how to use zenity to do that.

Option to create file selection dialog

The main option for creating a file selection dialog is --file-selection. When you enter the zenity command using this option, the file selection window will appear.

zenity-create-file-selection-dialog-01 Zenity create file selection dialog
Create file selection dialog

As shown in the image above, I will select the file named test.txt. After clicking OK, the selection window will be closed and a link to the file will be displayed on the terminal window.

zenity-create-file-selection-dialog-02 Zenity create file selection dialog
The result return when select a file

When you have the correct path to the file, your program may use other commands to read or write to the file.

Expand options

The options of --file-selection you can view in the image below or you type the following command to display the options:

zenity --help-file-selection
zenity-create-file-selection-dialog-03 Zenity create file selection dialog
Other options of file-selection

The options that are commonly used with --file-selection are listed below:

Expand options of file selection
OptionMeaning
--filename=FILENAMESpecifies the file or directory that is selected in the file selection dialog when the dialog is first shown.
--multipleAllows the selection of multiple filenames in the file selection dialog.
--directoryAllows only selection of directories in the file selection dialog.
--saveSet the file selection dialog into save mode.
--separator=SEPARATORSpecifies the string that is used to divide the returned list of filenames.

In the example image below, I opened two terminal windows side by side. The terminal on the left I use the --filename option with the value is Desktop while the right window with the value is Videos.

Read moreCreate a password entry dialog

zenity-create-file-selection-dialog-04 Zenity create file selection dialog
Set the filename

And you notice the results in the picture is different, on the left when the window opens, it has selected the Desktop folder first. While on the right, it selects the Videos folder first.

Similarly, try the other options of --file-selection, you can type the commands that I have listed below.

Use option --multiple to select multiple files as you want:

zenity --file-selection --multiple

Use option --directory if you want to select directories only:

zenity --file-selection --directory

When you use the --multiple option and select multiple files, for example, I would select the test.txt file and the test2.txt file. The results displayed on the terminal will look like this:

/home/danie/Desktop/test.txt|/home/danie/Desktop/test2.txt

If you see, a dash is used to separate the files. Now try the option --separator and see the result:

zenity --file-selection --separator=";"

And after you’ve tried all of the options above, try combining the options together in the same command to see what the results are.

Conclusion

Can you imagine that with just this simple option of zenity, what can you do with the bash script? Use it to write useful programs and share it with everyone. For example, you can write a simple program that lets you open a text file and edit content.

Continue reading the series«« Previous part: Zenity create a password entry dialogNext part: Zenity create a progress dialog »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.