How to call an extensions command from ruby script

I am working on automating a large amount of SketchUp files that require running a few commands from installed extensions.

Is there a way to find out the commands that are available in an extension and run them?

An example of one of the steps would be the last step of exporting the file into GLB using the SketchUp glTF Exporter by Evan Prananta. The command that I am looking to run is under Extensions > SKP glTF Exporter > Export Binary glTF 2.0

I am assuming this is a registered command somewhere withing the SketchUp environment, just need help finding out how to access it.

SketchUp has no direct way of invoking a specific command using the API. It might be possible to directly call whatever the command would call, but this requires knowledge of the internal workings of the extension. Even for an open source extension it’s fragile as method names can change, and for a proprietary extension the developer may not want you to poke around in there. You could however try reaching out to the author and see what they say. Maybe they are okay with exposing a set of simple commands publicly.

There is a clunky workaround to run a menu item.

  1. Assign a keychord shortcut via the Shortcuts panel of the Preferences dialog.

  2. Via the Ruby WIN32OLE library class, use the SendKeys method of the Windows Scripting Host’s Shell object to send the keychord to the SketchUp application. (The main window needs to have the focus in order for the keychords to be accepted.)

Example, say that you have assigned the keychord CTRL+F8 to the above menu item command:

module ShawnFrueh # <---<< top level namespace module
  module SomeNiftyTasks  # <---<< extension submodule

    extend self

    def export_gltf_binary
      require 'win32ole' unless defined?(WIN32OLE)
      shell = WIN32OLE.new('WScript.Shell')
      Sketchup.focus
      shell.SendKeys("{^F8}")
    end

    def do_task
      # ... preliminary stuff here ...
      export_gltf_binary()
    end

  end
end

The above is MS Windows platform only.

You can use utilities such as “cliclick” on Mac OS platform to send keychords to an application.


The Obvious: Since users subjectively assign shortcut keychords as they will, this paradigm is not well suited for public use in a distributed extension.

1 Like

:bulb: …if we’re talking about a clunky workaround:

The array of shortcut strings form Sketchup.get_shortcuts can be examined and find out what shortcut is assigned to the specific plugin…
(But still, this is not well suited.)

Edit add:
Perhaps:

glft = Sketchup.get_shortcuts.find{|s| s.include?("Export Binary glTF 2.0")}
glft_shortcut =glft.split(" ").first if glft

This will return a string in a format e.g. “Ctrl+F8” so the “Ctrl+” have to be replaced to ^

1 Like

Thanks for the info, I’ve reached out to the devs to see if they have some insight on how I can call their commands via ruby.