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.
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.
When you have the correct path to the file, your program may use other commands to read or write to the file.
The options of
--file-selection you can view in the image below or you type the following command to display the options:
The options that are commonly used with
--file-selection are listed below:
|Specifies the file or directory that is selected in the file selection dialog when the dialog is first shown.|
|Allows the selection of multiple filenames in the file selection dialog.|
|Allows only selection of directories in the file selection dialog.|
|Set the file selection dialog into save mode.|
|Specifies 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 more: Create a password entry dialog
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.
--multiple to select multiple files as you want:
zenity --file-selection --multiple
--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:
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.
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.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.
If you appreciate what we share in this blog, you can support us by:
- Stay connected to: Facebook | Twitter | Google Plus | YouTube
- Subscribe email to recieve new posts from us: Sign up now.
- Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
- Become a Supporter - Make a contribution via PayPal.
- Support us by purchasing Ribbon Lite Child theme being using on this website.
We are thankful for your support.