# Retrieving the filename from a GtkFileChooserDialog

**URL:** <https://discourse.julialang.org/t/retrieving-the-filename-from-a-gtkfilechooserdialog/25337>\
**Category:** General Usage\
**Created:** [June 16, 2019, 2:21am UTC](https://discourse.julialang.org/t/retrieving-the-filename-from-a-gtkfilechooserdialog/25337 "2019-06-16T02:21:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 16, 2019, 2:21am UTC](https://discourse.julialang.org/t/retrieving-the-filename-from-a-gtkfilechooserdialog/25337/1 "2019-06-16T02:21:43Z")

</div>

I’m looking to get the filename from a GtkFileChooserDialog and I feel like I’m close but I can’t figure out what I’m missing. I’ve read over:

> [@Gtk.jl - GTKFileChooserButton question](https://discourse.julialang.org/t/gtk-jl-gtkfilechooserbutton-question/24526/6):
>
> function on\_fileChooser\_file\_set(var) filename = Gtk.bytestring(Gtk.GAccessor.filename(var),true) println("this is the filename: ", filename) #println("I'm here.") end I cannot test because I don’t have the glade file. Best thing to test is that you provide a minimal working example (MWE). Instead of providing the glade file, you can also paste the content of the glade file into a string my\_ui\_string and call GtkBuilder(buffer=my\_ui\_string).

And that helped, but I’m getting:

```julia
ERROR: LoadError: MethodError: no method matching filename(::GtkFileChooserDialogLeaf)
Closest candidates are:
  filename(::GtkFileChooser) at /home/me/.julia/packages/Gtk/aP55V/gen/gbox3:3064
  filename(::GtkFileChooser, ::Any) at /home/me/.julia/packages/Gtk/aP55V/gen/gbox3:3067

```

When trying to call: `Gtk.bytestring(Gtk.GAccessor.filename(widget), true)`.

I’ve simplified an example down to the following two files, I hope it’s small enough to not annoy people.

First is `help.glade`:

```julia
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkFileChooserDialog" id="dialog">
    <property name="can_focus">False</property>
    <property name="type_hint">dialog</property>
    <property name="create_folders">False</property>
    <property name="preview_widget_active">False</property>
    <property name="use_preview_label">False</property>
    <child>
      <placeholder/>
    </child>
    <child internal-child="vbox">
      <object class="GtkBox">
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <property name="spacing">2</property>
        <child internal-child="action_area">
          <object class="GtkButtonBox">
            <property name="can_focus">False</property>
            <property name="layout_style">end</property>
            <child>
              <object class="GtkButton" id="cancel">
                <property name="label">gtk-cancel</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_underline">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="open">
                <property name="label">gtk-open</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <property name="use_underline">True</property>
                <property name="use_stock">True</property>
              </object>
              <packing>
                <property name="expand">True</property>
                <property name="fill">True</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">False</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkWindow" id="main">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="border_width">6</property>
    <property name="title" translatable="yes">Help</property>
    <property name="default_width">800</property>
    <property name="default_height">600</property>
    <property name="has_resize_grip">True</property>
    <signal name="destroy" handler="ui_destroy" swapped="no"/>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_File</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="add">
                        <property name="label">gtk-add</property>
                        <property name="name">menu_new</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkSeparatorMenuItem">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                      </object>
                    </child>
                    <child>
                      <object class="GtkImageMenuItem" id="quit">
                        <property name="label">gtk-quit</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
</interface>

```

Next the julia file:

```julia
#using Pkg
#Pkg.add("Gtk")

using Gtk

function display_ui()::GtkBuilder
    GtkBuilder(filename="help.glade")
end

function run(builder::GtkBuilder)::Channel

    local connectors = [
        ("quit", "activate", "quit"),
        ("add", "activate", "add"),
        ("dialog", "file-activated", "file"),
        ("dialog", "selection-changed", "file")
    ]

    Channel() do chan
        for (element, event, message) in connectors
            signal_connect(builder[element], event) do widget
                put!(chan, (message, widget))
            end
        end

        local c = Condition()
        signal_connect(builder["main"], :destroy) do widget
            notify(c)
        end
        wait(c)

    end
end

builder = display_ui()
channel = run(builder)

for (msg, widget) in channel
    println("Msg: ", msg)

    if msg == "add"
        showall(builder["dialog"])
    end

    if msg == "file"
        local filename = Gtk.bytestring(Gtk.GAccessor.filename(widget), true)
        println("Filename: ", filename)
    end

end

```

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 16, 2019, 11:08pm UTC](https://discourse.julialang.org/t/retrieving-the-filename-from-a-gtkfilechooserdialog/25337/2 "2019-06-16T23:08:55Z")

</div>

Figured it out if anyone runs across this later, ended up just needing to cast the widget to the correct type:

```julia
    if msg == "file"
        local val = Gtk.GAccessor.filename(Gtk.GtkFileChooser(widget))
        if val != C_NULL
            local filename = Gtk.bytestring(val)
            println("Filename: ", filename)
        end
    end

```
