Forum: Ruby Need Code to Create Directory Picking Dialog Box

Posted by Alex DeCaria (deke)
on 2010-02-09 18:18
I have a Ruby program and want to be able to pick a directory using a
dialog box.  Does anyone have a quick, easy piece of code that will
bring up a dialog box to pick a directory?  That's the only GUI part of
the program I need.  I don't want to learn a whole GUI programming
library just to create a directory picking dialog box, so if someone has
some code I can just plug into my existing Ruby program I would greatly
appreciate it.

Thanks!

Alex DeCaria
Posted by Marnen Laibow-Koser (marnen)
on 2010-02-09 21:04
Alex DeCaria wrote:
> I have a Ruby program and want to be able to pick a directory using a
> dialog box.  Does anyone have a quick, easy piece of code that will
> bring up a dialog box to pick a directory?  That's the only GUI part of
> the program I need.  I don't want to learn a whole GUI programming
> library just to create a directory picking dialog box, 

But you're going to have to.  The only way to do this is through a GUI 
library.

> so if someone has
> some code I can just plug into my existing Ruby program I would greatly
> appreciate it.
> 
> Thanks!
> 
> Alex DeCaria

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Bosko Ivanisevic (Guest)
on 2010-02-09 21:06
(Received via mailing list)
On Feb 9, 6:19 pm, Alex DeCaria <alex.deca...@millersville.edu> wrote:
> Alex DeCaria
> --
> Posted viahttp://www.ruby-forum.com/.

It really depends on which OS you are working and which library you
want to use. You can use ffi gem to open such dialog directly from
shared library or you can use some Ruby GUI bindings like RubyTk,
WxRuby or QtRuby. So there is no general solution and depends much of
your environment and your decision which library you will use.

Regards,
Bosko Ivanisevic
Posted by Alex DeCaria (deke)
on 2010-02-09 21:12
I figured out how to do it easily using FXRuby.  The code snippet below 
worked just fine and did exactly what I wanted.  Not sure it's the most 
efficient way, but it worked.

# ******** Directory Dialog Box *******************
require 'fox16'
include Fox

# Use GUI to select Session Directory
app = FXApp.new
dialog = FXDirDialog.new(app, "Select Session Directory")
dialog.directory = prs_root_path
app.create

dialog.show
if dialog.execute != 0 then
  session_directory = dialog.directory
else
  print "\n No session selected.  Program exiting.\n"
  exit
end
Posted by Albert Schlef (alby)
on 2010-02-09 22:26
Alex DeCaria wrote:
> I don't want to learn a whole GUI programming
> library just to create a directory picking dialog box

You can use a utility like 'dialog' or 'zenity' or 'kdialog':

  dir = `zenity --list --title "pick your dir" --column "dir" one two 
three`
  puts dir
Posted by Albert Schlef (alby)
on 2010-02-09 22:29
Albert Schlef wrote:
> You can use a utility like 'dialog' or 'zenity' or 'kdialog':
> 
>   dir = `zenity --list --title "pick your dir" --column "dir" one two 
> three`
>   puts dir

And after visiting its man page I see that you can just do:

  dir = `zenity --file-selection --directory`
  puts dir
Posted by Alex DeCaria (deke)
on 2010-02-09 22:34
Albert Schlef wrote:
> Albert Schlef wrote:
>> You can use a utility like 'dialog' or 'zenity' or 'kdialog':
>> 
>>   dir = `zenity --list --title "pick your dir" --column "dir" one two 
>> three`
>>   puts dir
> 
> And after visiting its man page I see that you can just do:
> 
>   dir = `zenity --file-selection --directory`
>   puts dir

Thanks Albert.  I didn't know about zenity or the other utilities.  That 
will come in handy.
Posted by Siep Korteling (steenslag)
on 2010-02-09 22:57
Alex DeCaria wrote:
> I figured out how to do it easily using FXRuby. (...)

Then this may be of no use to you (windows only):


require 'swin'
folder = SWin::CommonDialog.selectDirectory(nil, 'Pick your dir.', 
'C:/windows')
puts folder


Siep
Posted by Marnen Laibow-Koser (marnen)
on 2010-02-09 23:39
Alex DeCaria wrote:
> Albert Schlef wrote:
>> Albert Schlef wrote:
>>> You can use a utility like 'dialog' or 'zenity' or 'kdialog':
>>> 
>>>   dir = `zenity --list --title "pick your dir" --column "dir" one two 
>>> three`
>>>   puts dir
>> 
>> And after visiting its man page I see that you can just do:
>> 
>>   dir = `zenity --file-selection --directory`
>>   puts dir
> 
> Thanks Albert.  I didn't know about zenity or the other utilities.  That 
> will come in handy.

Neither did I.  Those look interesting.

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Alex DeCaria (deke)
on 2010-02-10 00:35
Siep Korteling wrote:
> Alex DeCaria wrote:
>> I figured out how to do it easily using FXRuby. (...)
> 
> Then this may be of no use to you (windows only):
> 
> 
> require 'swin'
> folder = SWin::CommonDialog.selectDirectory(nil, 'Pick your dir.', 
> 'C:/windows')
> puts folder
> 
> 
> Siep

Siep,

That is helpful as I am doing mostly windows work.  I didn't know about 
SWIN.  Thanks.  --Alex
Posted by Marnen Laibow-Koser (marnen)
on 2010-02-10 02:16
Alex DeCaria wrote:
> Siep Korteling wrote:
>> Alex DeCaria wrote:
>>> I figured out how to do it easily using FXRuby. (...)
>> 
>> Then this may be of no use to you (windows only):
>> 
>> 
>> require 'swin'
>> folder = SWin::CommonDialog.selectDirectory(nil, 'Pick your dir.', 
>> 'C:/windows')
>> puts folder
>> 
>> 
>> Siep
> 
> Siep,
> 
> That is helpful as I am doing mostly windows work.  I didn't know about 
> SWIN.  Thanks.  --Alex

You're probably best using something OS-neutral, though -- there's no 
reason to introduce an OS dependency for something this trivial n

Best,
-- 
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org
Posted by Alex DeCaria (deke)
on 2010-02-10 02:40
> 
> You're probably best using something OS-neutral, though -- there's no 
> reason to introduce an OS dependency for something this trivial n
> 
> Best,
> -- 
> Marnen Laibow-Koser
> http://www.marnen.org
> marnen@marnen.org

Good point.  Thanks for everyone's input!  You've all been very helpful.
--Alex
Posted by Albert Schlef (alby)
on 2010-02-10 21:41
Alex DeCaria wrote:
> I have a Ruby program and want to be able to pick a directory using a
> dialog box.  Does anyone have a quick, easy piece of code that will

And here's how to do it in Ruby/Tk:

  require 'tk'

  TkRoot.new.withdraw   # Get rid of the root window.

  dir = Tk.chooseDirectory
  puts dir


(There's also getOpenFile() and getSaveFile().)
Posted by Hidetoshi NAGAI (Guest)
on 2010-02-11 15:25
(Received via mailing list)
From: Albert Schlef <albertschlef@gmail.com>
Subject: Re: Need Code to Create Directory Picking Dialog Box
Date: Thu, 11 Feb 2010 05:41:38 +0900
Message-ID: <f7868ac33c342a61ecf9de78b68fb3d9@ruby-forum.com>
> Alex DeCaria wrote:
> > I have a Ruby program and want to be able to pick a directory using a
> > dialog box.  Does anyone have a quick, easy piece of code that will
> 
> And here's how to do it in Ruby/Tk:

And, if you hate that the eventloop wastes CPU power,
---------------------------------------------------------------
require 'tk'
Tk.root.withdraw

def choose_directory(keys = {})
  th = Thread.new{Tk.mainloop} # start the eventloop
  begin
    dir = Tk.chooseDirectory(keys) # show dialog
    Tk.update # for safety
  ensure
    th.kill # stop the eventloop
  end
  dir
end

if __FILE__ == $0
  dir = choose_directory(:initialdir => '/', :title => 'test test
  test')
  p dir
  p choose_directory(:initialdir => dir, :title => 'test TEST test')
end
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.