LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Browsing to a file (https://www.linuxquestions.org/questions/linux-desktop-74/browsing-to-a-file-4175630123/)

keirvt 05-20-2018 10:25 PM

Browsing to a file
 
I have a bash script that I want users (from a Windows background) to run run by clicking on the desktop. Users can plug in a USB and copy a file to the desktop and then click on the script icon to run the script.

So far no problem.

I need a command similar to the Windows "browse to file" window that allows users to select some file for use in an application.
In this case I wish the browse utility to be executed from the bash script and then the name of the file selected by the user returned to the script so that it may then process that file.

Is there a way to do this?

michaelk 05-21-2018 04:57 AM

Depends on desktop but there are dialog boxes that you can use in your bash script that are GUI like. zenity for Gnome based, Kdialog for KDE or basic ncurses dialog are a few options

fatmac 05-21-2018 07:36 AM

Would it not be easier for your script to open a file manager for them to use?

BW-userx 05-21-2018 08:53 AM

bash scripts and not something like gtk gui stuff. I'd suggest trying to get it to invoke a terminal to gain input then exiting the terminal whence it retrieve the input, then going on to the next step. Can it be done? In theory. Absolutely.

whats that midnight commander terminal driven File Manager, what it got you might be able to incorporate into your script?

keirvt 05-22-2018 05:48 PM

Browse to file
 
The idea is to get a windows GUI based person to execute a program and within a gnome-terminal. They have to select a file from their system and it is not desirable that they type the name of the file because their files might have spaces or apostrophes and most users can't type etc etc etc.


So the user clicks on a desktop icon to run a script. They already have access to the file manager, running file manager from the script is no help.

Zenity file selection is no better than getting the user to type the file name in because it doesn't seem to have and file selection feature and examples require tping the file name in. (Pity)

The best method I could come up with is calling a Python program from the bash script and assigning the returned file name to a variable in a shell script to run further applications that process that file.

Code:

from Tkinter import Tk
from tkFileDialog import askopenfilename

Tk().withdraw()
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)


BW-userx 05-22-2018 05:53 PM

why you trying to bottle feed them a with a strange nipple to suck on, and just let them get use to using Linux the Linux way?

tofino_surfer 05-27-2018 03:27 PM

Quote:

Zenity file selection is no better than getting the user to type the file name in because it doesn't seem to have and file selection feature and examples require typing the file name in. (Pity)
I just tried this myself and zenity opens up the GTK file dialog. I am able to navigate through my disks with the GUI. When I double click on a filename the window closes and the selected filename appears on the terminal output. This can also be stored in a shell variable such as:

Code:

04:24 PM $ FILE=$(zenity --file-selection)
Gtk-Message: GtkDialog mapped without a transient parent. This is discouraged.
04:30 PM $ echo $FILE
/home/andrew/Olympics/PyeongChang2018/NBC_ClosingCeremony.mp4

I was able to navigate two levels down to select a file whose path is then stored in the variable FILE.

Quote:

$ zenity --help-file-selection
Usage:
zenity [OPTION…]

File selection options
--file-selection Display file selection dialogue
--filename=FILENAME Set the filename
--multiple Allow multiple files to be selected
--directory Activate directory-only selection
--save Activate save mode
--separator=SEPARATOR Set output separator character
--confirm-overwrite Confirm file selection if filename already exists
--file-filter=NAME | PATTERN1 PATTERN2 ... Set a filename filter


keirvt 05-28-2018 03:50 AM

Zenity and Bottle Feeding
 
The Zenity solution is a good answer to the problem I didn't research Zenity very much and missed some of its features. Yes it is a good solution - thank you for that.

The script I was running ran a Pythonm program anyway so I still used the Python solution since all I had to do was add few line to that program. Nevertheless Zenity is another tool I will bear in mind for future problems.

As to bottle feeding - I work in a workplace where management has this amazingly frustrating view that all software should be developed and run on a Windows platform. The minute you start expecting users to type things in long hand or depart from the Windows way of suffering I am pushed back a step on the journey of enlightenment.

In any case, software should be seamless and operationally obvious to users.


All times are GMT -5. The time now is 08:59 PM.