Zenity create file selection dialog

by Daniel Pham
Published: Updated:
This entry is part 6 of 13 in the series GUI for bash script using Zenity

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
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
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
Other options of file-selection

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

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

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series Navigation«« Previous part: Zenity create a password entry dialogNext part: Zenity create a progress dialog »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.