SketchUp Ruby coding a dialog

Hallo there!

I’m new to coding in Ruby for sketch up and have a whole number of questions! Still, I used to do some coding before and currently understand most of coding aspects.

  1. Is it possible to create an interface within SketchUp just using Ruby or do I have to use WebDialog/HtmlDialog? I need to make buttons/tables/ect.
  2. I tried the following code, but it just created a new WebDialog:
wd = UI::WebDialog.new "Point Checker"

wd.show

wd.execute_script("alert('Hello from Web Dialog')");

It seens like it didn’t implement wd.execute_script(“alert(‘Hello from Web Dialog’)”);

  1. It’s really hard to learn about Ruby and Java Script cooperation in detail, as far as I can’t find any simple step by step explanation of that. I’ve found some written tutorials and they worked for me. I could establish this Ruby/Java Script relation, but the code was more complicated and I don’t quite understand, how it really worked. There are some completely different examples and no really good explanation to the code on the Internet. I’m reading Automatic SketchUp. It’s a fantastic book! However, the code shown in the book is a way different from what really worked in my programme! This simple “wd.execute_script(“alert(‘Hello from Web Dialog’)”);” returns nothing. How do you guys learn this stuff? There no video tutorials or books. This Ruby/JS relation to me is like a rare science. There are so many aspects of it, and so few keys to use. Anyway, thank you, all, folks, who promote this knowledge to people!

Check the wiki’s by @DanRathbun :

Please see this “pinned thread” on how to post code in these forums …

Javascript cannot execute in a vacuum !
It can only be done within the context of a HTML document.
So first you must load a document by either creating a file, or a string of HTML text:

wd = UI::WebDialog.new "Point Checker"

htmltext = '<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <b>Hello world!</b>
  </body>
</html>
'

wd.show

wd.set_html(htmltext)

Then AFTER the document is loaded and rendered in the browser frame, you can execute javascript.
(You can also set the document before you show the dialog, but it actually loads when you show it in that case.)

Also the semicolon is misplaced. Ruby does not need semicolons at the end of lines.

I would put the semicolon at the end of the javascript line instead:

wd.execute_script("alert('Hello from Web Dialog');")

UI::WebDialog is deprecated and you should use UI::HtmlDialog if using SketchUp 2017 or higher.

Depending on your requirements, you might be able to make use of the Toolbar class:

There’s also the much simpler UI:InputBox. It can handle typed text input, or picking from a drop down list, in any number of fields.

1 Like

Thank you so much for your replies! These are really useful!

We recently published some examples for using UI::HtmlDialog: GitHub - SketchUp/htmldialog-examples: Examples of using SketchUp Ruby API's UI::HtmlDialog class
It includes seven examples.

However, it focuses on using third party library to speed up development. If you are brand new to Ruby and JS, it might not be ideal to dive into a third party framework as well. But just posting a link here for future reference.

As @john_mcclenahan mentions, there is UI.inputbox - it’s not pretty. But it gives you basic user input. I can be a good place to start if you are learning. Allowing you to focus on one thing at a time.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.