Tuesday, May 14, 2024
39
rated 0 times [  39] [ 0]  / answers: 1 / hits: 33494  / 3 Years ago, thu, may 13, 2021, 12:15:05

Suppose I have a script like this:



(The example depicts an rysnc use case)



#!/bin/bash
echo -n "Enter Source Directory:"
read srcdir
echo -n "Enter Destination Directory:"
read dstdir
rsync -av --delete "$srcdir" "$dstdir"


The idea here is to prompt the user to enter the "Source" and "Destination" directories for rsync to work with. As is, the user will have to manually enter /path/to/directory/ via the command-line.



Instead, I want to prompt the user to enter the paths through a GUI interface.



Something like this:
screem






What commands can I use to prompt the user with a GUI selection window that returns the file path to the command-line?


More From » command-line

 Answers
5

You can use this for files:



zenity --file-selection


and this for folders:



zenity --file-selection --directory


for usage, run:



zenity --help-general
zenity --help-file-selection


Generally it matches the current theme (for GTK window managers anyway), on my machine with a modded version of Zukitwo 3.8 it looks like this:



One way of using it is like this:



echo "you selected $(zenity --file-selection)"


Which would result in you selected /path/to/file.



You can also use options to set an appropriate title, and the directory it starts in - With your rsync use case, for example:



zenity --file-selection --directory --title="Choose rsync source directory" --filename=$HOME/Desktop/


For files, you can also specify a filetype to select - e.g:



zenity --file-selection --file-filter='PDF files (pdf) | *.pdf' --title="Select a PDF file"


NOTE: You can also use YAD, a fork of Zenity that has loads more features.



sudo add-apt-repository ppa:webupd8team/y-ppa-manager
sudo apt-get update
sudo apt-get install yad


Source



For the most part you can use it the same way - for the file browser:



yad --file-selection


and for the help page:



yad --help-all


Though at the time (around version 26?), it had not been updated to match the new GTK 3.14+ interface (zenity had) - it has more features, but check compatibility (based on documentation it should work on GTK+ >= 2.16.0


[#24425] Friday, May 14, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pilun

Total Points: 270
Total Questions: 100
Total Answers: 94

Location: England
Member since Sat, Feb 13, 2021
3 Years ago
pilun questions
Thu, Sep 16, 21, 18:43, 3 Years ago
Mon, Aug 23, 21, 04:32, 3 Years ago
Wed, Sep 14, 22, 11:04, 2 Years ago
;