# Using HtmlDialog.set\_file(filename)

**URL:** <https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780>\
**Category:** Ruby API\
**Created:** [February 2, 2022, 8:33pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780 "2022-02-02T20:33:57Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 2, 2022, 8:33pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/1 "2022-02-02T20:33:57Z")

</div>

I want to use an existing HTML file (`test1.html`) in an HtmlDialog.

I’ve got it to work using the Ruby `heredoc` construct and cut and pasted the html file contents into the Ruby file that way.

It works fine on its own when opening in Chrome, and did work just as well inside SU when included as a `heredoc`.

I’m using a slightly adapted version of an HtmlDialog example from the Sketchup GitHub pages (the Step03 example).

But I can’t see how to use the file itself using `dialog.set_file(filename)`

The ruby file that loads the example dialog is:

```ruby
module JWMPlugins
module Test2

  # Window fully reusable.

  def self.create_dialog
    options = {
      :dialog_title => "Example",
      :preferences_key => "example.htmldialog.test",
      :style => UI::HtmlDialog::STYLE_DIALOG
    }
    dialog = UI::HtmlDialog.new(options)
    dialog.center
    dialog
  end

  def self.show_dialog
     if @dialog && @dialog.visible?
      @dialog.bring_to_front
    else
      # Attach content and callbacks when showing the dialog,
      # not when creating it, to be able to use the same dialog again.

      @dialog ||= self.create_dialog
      @dialog.add_action_callback('poke') { |action_context, name, num_pokes|
        self.on_poke(name, num_pokes)
        nil
      }
      @dialog.set_file("test1.html")
      @dialog.show
     end
  end

  def self.on_poke(name, num_pokes)
    num_pokes.times {
      puts "Poke #{name}!"
    }
  end

end # Test2
end # JWMPlugins

```

The `test1.html` file is in the Sketchup `Plugins/test` folder, the same folder as `test2.rb`, and there’s nothing else in the `test` folder.

But I can’t get the dialog code to open this file.

All that shows is a blank white window with (as set up in `options`) the window title `Example`.

![image](https://global.discourse-cdn.com/sketchup/original/3X/0/0/00fc373f3d83d4d323b02e1e6d748aee846d1aab.png)

I’ve tried `@dialog.set_file("./test1")` and `@dialog.set_file("test/test1")` but that neither of those works either.

And there are no errors in the Ruby console.

I expect it’s something simple that I’m misunderstanding or just ‘doing wrong’ but I can’t see what it is myself.

Any suggestions please?

The API Docs example shows a full path to the file, but is that necessary?

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 2, 2022, 8:48pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/2 "2022-02-02T20:48:20Z")

</div>

```auto
@dialog.set_file(File.dirname( __FILE__ ) + "/test1.html")

```

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 2, 2022, 8:50pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/3 "2022-02-02T20:50:42Z")

</div>

Many thanks for such a quick response, @dezmo.

That works, and I can see why, though not quite why my code didn’t.

Why does Ruby not default to looking the current directory? I’d expect it to look in either the Plugins, or Plugins/test folders.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 2, 2022, 9:11pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/4 "2022-02-02T21:11:24Z")

</div>

As far as I remember Ruby will use this directories to try to look for files:

```auto
ENV["PATH"]

```

Normally the `Plugins` are not in that list.  
Why is this so? Because that’s how someone created it … 😇

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 2, 2022, 9:12pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/5 "2022-02-02T21:12:58Z")

</div>

> [@dezmo](#):
>
> Why is this so? Because that’s how someone created it …

But if I remember, ruby `require` doesn’t need to be told the directory, just a file path relative to Plugins.

---

<div class="post-metadata">

**Author:** ![dezmo](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/dezmo/32/51058_2.png) [@dezmo](https://forums.sketchup.com/u/dezmo)\
**Post date:** [February 2, 2022, 9:23pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/6 "2022-02-02T21:23:24Z")

</div>

That is right the `require` first start to look for relative path. Again, I’m not sure why it is like this…  
Maybe someone else will explain… 😊

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 3, 2022, 1:03am UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/7 "2022-02-03T01:03:17Z")

</div>

> [@john\_mcclenahan](#):
>
> Why does Ruby not default to looking the current directory?

It does, but the “Plugins” directory or any possible extension subdirectory, is **not** the current working directory. When SketchUp loads up it sets the current directory to the user’s HOME or Documents path. (The former in older versions, the latter since ~v2014.)

To see the current working directory path use:

```ruby
Dir.pwd

```

… or …

```ruby
Dir.getwd

```

But any extension might change the current working directory and forget to change it back, so **never** rely upon what you think it should be. Instead temporarily change it using the **block form** of [`Dir::chdir`](https://ruby-doc.org/core-2.7.2/Dir.html#method-c-chdir) so that it is restored to what it was before.

```ruby
  def self.show_dialog
    if @dialog && @dialog.visible?
      @dialog.bring_to_front
    else
      # Attach content and callbacks when showing the dialog,
      # not when creating it, to be able to use the same dialog again.

      @dialog ||= self.create_dialog
      @dialog.add_action_callback('poke') { |action_context, name, num_pokes|
        self.on_poke(name, num_pokes)
        nil
      }
      # Temporarily change the working directory:
      Dir::chdir( __dir__ ) {
        @dialog.set_file("test1.html")
      }
      @dialog.show
     end
  end

```

> [@john\_mcclenahan](#):
>
> I’d expect it [_Ruby_] to look in either the Plugins, or Plugins/test folders.

Ruby does **not** know anything specific about SketchUp, any of it’s various “Plugins” paths nor any of it’s extensions or resources paths. Ruby has basically been “shoehorned” into SketchUp.

> [@john\_mcclenahan](#):
>
> But if I remember, ruby `require` doesn’t need to be told the directory, just a file path relative to Plugins.

Ah, this is where you are confusing yourself. Ruby’s [`Kernel#require`](https://ruby-doc.org/core-2.7.2/Kernel.html#method-i-require) checks the paths listed in it’s **`$LOAD_PATH`** array. In order to make Ruby work with SketchUp, the startup cycle pushes various SketchUp extension paths onto the end of the **`$LOAD_PATH`** array, before it begins processing any of the various “Plugins” folders.

* * *

So the bottom line is, either use full absolute paths, use `File.join( __dir__ , "somefile.ext")` or temporarily change the working directory as shown above.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 3, 2022, 2:07am UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/8 "2022-02-03T02:07:46Z")

</div>

Dan, many thanks for your clear explanation.

If I don’t need the directory for any other reason than to load the `test1.html` file, do I need to know the working directory?

---

<div class="post-metadata">

**Author:** ![DanRathbun](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/danrathbun/32/500_2.png) [@DanRathbun](https://forums.sketchup.com/u/DanRathbun)\
**Post date:** [February 3, 2022, 5:11am UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/9 "2022-02-03T05:11:11Z")

</div>

> [@john\_mcclenahan](#):
>
> If I don’t need the directory for any other reason than to load the `test1.html` file, do I need to know the working directory?

Not really, if you use the block form of `Dir::chdir`, it restores it to whatever it was before.

But also you can just forego the working dir altogether and use an absolute full path …

```ruby
@dialog.set_file(File.join( __dir__ , 'test1.html'))

```

The changing of the working directory is good when code would be doing several to many file reads and writes to the same path.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 3, 2022, 11:37am UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/10 "2022-02-03T11:37:30Z")

</div>

Understood. Tx again Dan

---

<div class="post-metadata">

**Author:** ![tt\_su](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tt_su/32/17_2.png) [@tt\_su](https://forums.sketchup.com/u/tt_su)\
**Post date:** [February 8, 2022, 3:29pm UTC](https://forums.sketchup.com/t/using-htmldialog-set-file-filename/188780/11 "2022-02-08T15:29:51Z")

</div>

+1 to resolving absolute paths from the current source file using ` __FILE__ ` or ` __dir__ ` over changing working directory. I’ve long recommended that over anything else. Some often use `Sketchup.find_support_file`, but that also have potential problems. Old blog post: [SketchUp Plugins can be Installed Anywhere | Procrastinators Revolt!](http://www.thomthom.net/thoughts/2012/09/sketchup-plugins-can-be-installed-anywhere/)
