Send a text from JavaScript to Ruby and receive a different one in the same JavaScript function

I’m trying to send a text from a java function to ruby. This will be the input of a ruby function that gerenare another text.
I’m having problems because I don’t know how to send one text and return a different one to the same java function.
This will be a simplified example:

function myFunction(text1) {
    alert("Received: " + suposedTextReturned)

This function send “text1” to ruby.

dialog.add_action_callback("test1") {|action_context, test1|
  suposedTextReturned = "This text was received from ruby function"

This callback generates “suposedTextReturned”

How can I use this text again in the java function in this line?

alert("Received: " + suposedTextReturned)

I tried with return suposedTextReturnedbut is not working


function myFunction(text1) {
  sketchup.test1(test1, {
    onCompleted: (ruby_text) => {
      alert("Received: " + ruby_text)

The text returned to javascript will be whatever the result of the last executed statement in the add_action_callback block/proc. So, you could use:

dialog.add_action_callback("test1") { |_action_context, test1|
  "Sent by Ruby, returning #{text1}"
1 Like

Yes, it worked!
I tried with return at the end of ruby callback but there was an error. With a string at the end there is no problem.
Thanks a lot @MSP_Greg

The Chromium HTML dialog windows use JavaScript, not Java (which is a different yet related coding language.)

FYI, the Ruby language name is capitalized.

Actually, not. This JavaScript function is sending the value of test1 not text1 to Ruby.
Ie, … the JS argument and the Ruby callback argument have mismatched identifier names.

Greg’s example also parroted this error.

return only works from methods.

But it is optional at the end of a block because (as said) the value of the last statement will be returned anyway.

1 Like


Good catch.

A few weeks ago I wrote an HtmlDialog benchmark testing two vectors
[‘execute_script’, ‘on_completed’] and [‘DOM’, ‘DOM into DocumentFragment’, ‘innerHTML’]. All six combinations performed at about the same speed, so the choice is immaterial. That might not be the case with large and complex DOM manipulations…

Hence, I just copied some code from that, and didn’t look at the posted example or run the code…

Re return, it also works in lambdas. See Ruby docs Class: Proc ‘Lambda and non-lambda semantics’

test1 is what I needed to send but in my final code I wrote the same word in both “text1” and “test1” and It worked. I will pay more attention next time

That means that in this quoted lines “ruby_test” is doing nothing?

ruby_test would be the return value from Ruby … ie, WHATEVER the value is that is returned from the Ruby test1 callback block.

For example …

dialog.add_action_callback("test1") { |_action_context, test1|
  "Sent by Ruby, returning #{test1}"

if test1 on the JS-side was set to be "Hello" via …


… then the alert popup in JS would display "Received: Sent by Ruby, returning Hello".

But you can return any string that you need from the Ruby-side, not just what the JS-side originally sent to Ruby.

1 Like