sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    🫛 Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    Multiple Action Callbacks?

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 2 Posters 367 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M Offline
      MartinRinehart
      last edited by

      The docs don't say you can do this, but in my first test, it worked. Anybody using this in production code?

      
      # selectionInfo.rb
      require 'sketchup.rb'
      
      # Create the WebDialog instance
      wd = UI;;WebDialog.new("Selection Info", false, "Selection Info", 200, 200, 200, 200, true)
      
      # Attach an action callback
      wd.add_action_callback("refresh") do | js_wd, ignored_param |
      
      	total_selected = Sketchup.active_model.selection.length
      	script = "rubyReturned("+ total_selected.to_s + ")"
      	js_wd.execute_script( script )
      
      end
      
      wd.add_action_callback("lie") do | js_wd, ignored_param |
      	js_wd.execute_script( "rubyReturned( 'A million' )" )
      end
      
      # Find and show our html file
      wd.set_file( 'c;/models/rubies/selectionInfo.html' )
      wd.show()
      
      

      You'll need to change that hard-wired path to your own.

      
      <!-- selectionInfo.html -->
      <html> <head> <script>
      
      function rubyCalled( nop ) {
      	query = 'skp;refresh@' + nop;
      	window.location.href = query;
      }
      
      function rubyLies( nop ) {
      	query = 'skp;lie@' + nop;
      	window.location.href = query
      }
      
      function rubyReturned( value ) {
      	var message = value + " items selected.";
      	document.getElementById('output').innerHTML = message;
      }
      
      </script> </head>
      
      <body onload='rubyCalled("refresh")'>
      
      <div align=center> <h3 id="output"> </h3>
      <button onclick="rubyCalled('this_is_ignored')">Refresh</button>
      <button onclick="rubyLies('this_too_is_ignored')">Tell Lie</button> </div>
      
      </body> </html>
      
      

      (Thank you, Scott Lininger, for the original code.)

      Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        I was not aware of any limitation on the number of callbacks. Out of curiosity, where did you read that?

        Hi

        1 Reply Last reply Reply Quote 0
        • M Offline
          MartinRinehart
          last edited by

          @jim said:

          I was not aware of any limitation on the number of callbacks. Out of curiosity, where did you read that?

          I had assumed that you needed a switch in "the" action callback for various options. Didn't know that the "get_data" part of 'skp:get_data@'... from Scott's how-to was a variable.

          And where does one report that the invaluable link to Scott's article in the doc is simple text, not a link?

          Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

          1 Reply Last reply Reply Quote 0
          • J Offline
            Jim
            last edited by

            One technique I have used is to pass the name of the Ruby callback to the Javascript. In many cases, you can use a single Javascript function to call any number of Ruby callbacks.

            
            function callRuby(callback_name, parameters) {
              window.location="skp;"+callback_name+"@"+paramters;
            }
            <input type="button" onclick="callRuby('draw_square', 10);" value="Draw a Square">
            
            

            Hi

            1 Reply Last reply Reply Quote 0
            • M Offline
              MartinRinehart
              last edited by

              @jim said:

              One technique I have used is to pass the name of the Ruby callback to the Javascript. In many cases, you can use a single Javascript function to call any number of Ruby callbacks.

              Neat trick, and that tells me exactly what I wanted to know. Now where to we go to get the docs to say "add one or more callbacks ..."?

              Author, Edges to Rubies - The Complete SketchUp Tutorial at http://www.MartinRinehart.com/models/tutorial.

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement