# Need Code to Create Directory Picking Dialog Box

**URL:** <https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345>\
**Category:** ruby-talk\
**Created:** [9 February 2010 17:19 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345 "2010-02-09T17:19:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [9 February 2010 17:19 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/1 "2010-02-09T17:19:01Z")

</div>

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 via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [9 February 2010 20:04 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/2 "2010-02-09T20:04:53Z")

</div>

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](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Bosko\_Ivanisevic](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bosko\_Ivanisevic](https://rubytalk.org/u/Bosko_Ivanisevic)\
**Post date:** [9 February 2010 20:05 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/3 "2010-02-09T20:05:10Z")

</div>

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

> **···**
>
> On Feb 9, 6:19 pm, Alex DeCaria \<alex.deca...@millersville.edu\> 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, so if someone has  
> > some code I can just plug into my existing Ruby program I would greatly  
> > appreciate it.
> > 
> > Thanks!
> > 
> > Alex DeCaria  
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [9 February 2010 20:12 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/4 "2010-02-09T20:12:23Z")

</div>

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  
&nbsp;&nbsp;session\_directory = dialog.directory  
else  
&nbsp;&nbsp;print "\n No session selected. Program exiting.\n"  
&nbsp;&nbsp;exit  
end

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [9 February 2010 21:26 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/5 "2010-02-09T21:26:56Z")

</div>

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':

&nbsp;&nbsp;dir = `zenity --list --title "pick your dir" --column "dir" one two  
three`  
&nbsp;&nbsp;puts dir

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [10 February 2010 20:41 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/6 "2010-02-10T20:41:38Z")

</div>

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:

&nbsp;&nbsp;require 'tk'

&nbsp;&nbsp;TkRoot.new.withdraw # Get rid of the root window.

&nbsp;&nbsp;dir = Tk.chooseDirectory  
&nbsp;&nbsp;puts dir

(There's also getOpenFile() and getSaveFile().)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [9 February 2010 21:29 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/7 "2010-02-09T21:29:12Z")

</div>

Albert Schlef wrote:

> You can use a utility like 'dialog' or 'zenity' or 'kdialog':
> 
> &nbsp;&nbsp;dir = `zenity --list --title "pick your dir" --column "dir" one two  
> three`  
> &nbsp;&nbsp;puts dir

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

&nbsp;&nbsp;dir = `zenity --file-selection --directory`  
&nbsp;&nbsp;puts dir

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Siep\_Korteling](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@Siep\_Korteling](https://rubytalk.org/u/Siep_Korteling)\
**Post date:** [9 February 2010 21:59 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/8 "2010-02-09T21:59:49Z")

</div>

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 via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [11 February 2010 14:24 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/9 "2010-02-11T14:24:39Z")

</div>

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,

> **···**
>
> 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  
> ---------------------------------------------------------------  
> require 'tk'  
> Tk.root.withdraw
> 
> def choose\_directory(keys = {})  
> &nbsp;&nbsp;th = Thread.new{Tk.mainloop} # start the eventloop  
> &nbsp;&nbsp;begin  
> &nbsp;&nbsp;&nbsp;&nbsp;dir = Tk.chooseDirectory(keys) # show dialog  
> &nbsp;&nbsp;&nbsp;&nbsp;Tk.update # for safety  
> &nbsp;&nbsp;ensure  
> &nbsp;&nbsp;&nbsp;&nbsp;th.kill # stop the eventloop  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;dir  
> end
> 
> if \_\_FILE\_\_ == $0  
> &nbsp;&nbsp;dir = choose\_directory(:initialdir =\> '/', :title =\> 'test test  
> &nbsp;&nbsp;test')  
> &nbsp;&nbsp;p dir  
> &nbsp;&nbsp;p choose\_directory(:initialdir =\> dir, :title =\> 'test TEST test')  
> end  
> ---------------------------------------------------------------  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [9 February 2010 21:34 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/10 "2010-02-09T21:34:45Z")

</div>

Albert Schlef wrote:

> Albert Schlef wrote:
> 
> > You can use a utility like 'dialog' or 'zenity' or 'kdialog':
> > 
> > &nbsp;&nbsp;dir = `zenity --list --title "pick your dir" --column "dir" one two  
> > three`  
> > &nbsp;&nbsp;puts dir
> 
> And after visiting its man page I see that you can just do:
> 
> &nbsp;&nbsp;dir = `zenity --file-selection --directory`  
> &nbsp;&nbsp;puts dir

Thanks Albert. I didn't know about zenity or the other utilities. That  
will come in handy.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [9 February 2010 23:35 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/11 "2010-02-09T23:35:52Z")

</div>

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 via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [9 February 2010 22:39 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/12 "2010-02-09T22:39:43Z")

</div>

Alex DeCaria wrote:

> Albert Schlef wrote:
> 
> > Albert Schlef wrote:
> > 
> > > You can use a utility like 'dialog' or 'zenity' or 'kdialog':
> > > 
> > > &nbsp;&nbsp;dir = `zenity --list --title "pick your dir" --column "dir" one two  
> > > three`  
> > > &nbsp;&nbsp;puts dir
> > 
> > And after visiting its man page I see that you can just do:
> > 
> > &nbsp;&nbsp;dir = `zenity --file-selection --directory`  
> > &nbsp;&nbsp;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](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Marnen\_Laibow-Koser](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@Marnen\_Laibow-Koser](https://rubytalk.org/u/Marnen_Laibow-Koser)\
**Post date:** [10 February 2010 01:16 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/13 "2010-02-10T01:16:37Z")

</div>

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](http://www.marnen.org)  
> marnen@marnen.org  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_DeCaria](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Alex\_DeCaria](https://rubytalk.org/u/Alex_DeCaria)\
**Post date:** [10 February 2010 01:41 UTC](https://rubytalk.org/t/need-code-to-create-directory-picking-dialog-box/57345/14 "2010-02-10T01:41:01Z")

</div>

> 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](http://www.marnen.org)  
> marnen@marnen.org

Good point. Thanks for everyone's input! You've all been very helpful.  
--Alex

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
