Use the ruby code to simulate a mouse click on the sub-menu in menu

Hello, in the sketchup ruby API, can I use the ruby code to simulate a mouse click on the sub-menu in menu?
thank you very much!

This can’t be done in Ruby alone. Maybe it can be done with OS specific libraries in other languages but I don’t think it is a good idea. Menu items can be moved or renamed and their names are language specific. What items are available, and thus their indices, also heavily depends on what is selected and has several combinations.

Rather than trying to target a part of the GUI I’d suggest to instead re-create the underlying action in Ruby.

@haiquan What menu item do you want access to ?

Such as ‘File - Open - choose .skp - save’. Instead of using code

Sketchup.open_file(skp)
Sketchup.active_model.save

can Ruby simulate this:
** access to** the menu ‘File’, then move to ‘Open’ → ‘Choose .skp’ → click Open → etc …
Just for example, maybe he wants to slick different menu, but I think there are the same thing. Thx :slight_smile:

I know how to do this on MS Windows …

Search the Ruby API category on “Win32OLE SendKeys”

Here is one post of mine … How to set API Export IFC Options? - #2 by DanRathbun

For Mac search the forum for “cliclick”.

Your code works in my su, and i understand how to send key to su.
But when i assigned shortcut Ctrl Alt Shift T to Thea Render/Create extendal model/proxy, i can’t use this fun to change anything.

require 'win32ole'
def test()
    shell = WIN32OLE::new("WScript.Shell")
    shell.SendKeys "^+%(T)"
end
# nothing happened...

I look up in SendKeys Method and i find that Ctrl Alt Shift T → “^+%(T)”, am i right?

Then i write this in su Ruby Panel:

shell = WIN32OLE::new("WScript.Shell")
shell.SendKeys "^(O)"
shell.SendKeys "^(N)"

Since Ctrl O is to open select .skp window, etc…, but nothing happens. Im confused:

Did i code wrong, or i can’t send shortkey to su, or something else?

But thank you anyway !!!:grinning:

You cannot test SendKeys from the Ruby Console window because the console window will receive the keys instead of the SketchUp application window.

Yes. (I think T should work the same as t.)

Try this …

module LTC
  module TheaShortcut

    extend self
    require 'win32ole'

    def test()
      puts "In method 'test', sending keys CTRL+ALT+SHIFT+T ..."
      shell = WIN32OLE::new("WScript.Shell")
      shell.SendKeys "^+%(t)"
    end

    UI.add_context_menu_handler {|popup|
      popup.add_item("Test Thea Shortcut") { test() }
    }

  end
end

Then use the right-click context menu item to test from the main SketchUp window.

:bulb:

1 Like