LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 05-20-2018, 10:25 PM   #1
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 134

Rep: Reputation: 17
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?
 
Old 05-21-2018, 04:57 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 17,599

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
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
 
Old 05-21-2018, 07:36 AM   #3
fatmac
Senior Member
 
Registered: Sep 2011
Location: Upper Hale, Surrey/Hants Border, UK
Posts: 2,626

Rep: Reputation: Disabled
Would it not be easier for your script to open a file manager for them to use?
 
Old 05-21-2018, 08:53 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,884

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
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?

Last edited by BW-userx; 05-21-2018 at 08:56 AM.
 
Old 05-22-2018, 05:48 PM   #5
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 134

Original Poster
Rep: Reputation: 17
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)
 
Old 05-22-2018, 05:53 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro / Parrot
Posts: 6,884

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
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?
 
Old 05-27-2018, 03:27 PM   #7
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 284

Rep: Reputation: 82
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

Last edited by tofino_surfer; 05-27-2018 at 03:37 PM.
 
Old 05-28-2018, 03:50 AM   #8
keirvt
Member
 
Registered: Sep 2006
Location: Sydney Australia
Distribution: fedora/Ubuntu
Posts: 134

Original Poster
Rep: Reputation: 17
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.
 
  


Reply

Tags
bash, file selector


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Gnome file browsing kitek Linux - Newbie 3 04-13-2012 12:43 PM
File Browsing Suddenly slow - 'exports' file? Toods Slackware 2 06-29-2008 03:29 PM
File browsing velan Programming 8 05-17-2005 12:45 PM
FILE browsing and XFS crispyleif Linux - General 6 03-11-2005 11:36 PM
file browsing tedivm Linux - Newbie 3 08-18-2004 12:43 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 09:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration