Ruby to add description to dynamic component

I’m finding my way to dynamic components and have created quite a bunch of them. Now I would like to add a description to the DC’s, that’s visible in the dynamic components tray. I tried Ruby script, but I’m new to that. I tried AI, which came up with:

require ‘sketchup.rb’

module DCDescription

def self.set_description
model = Sketchup.active_model
selection = model.selection

if selection.length != 1 || !selection.first.is_a?(Sketchup::ComponentInstance)
  UI.messagebox("Selecteer precies één component.")
  return
end

comp = selection.first
defn = comp.definition

prompts = ["Voer description in:"]
defaults = [defn.description.to_s]
input = UI.inputbox(prompts, defaults, "Component Description")

if input
  defn.description = input[0]
  UI.messagebox("Description opgeslagen.")
end

end

unless file_loaded?(__FILE__)
cmd = UI::Command.new(“Set Component Description”) {
DCDescription.set_description
}

# Iconen (moeten in dezelfde map staan als dit .rb bestand)
cmd.small_icon = File.join(__dir__, "icon_16.png")
cmd.large_icon = File.join(__dir__, "icon_24.png")

cmd.tooltip = "Set Component Description"
cmd.status_bar_text = "Pas de description van een component aan"

toolbar = UI::Toolbar.new("Component Tools")
toolbar.add_item(cmd)
toolbar.show

file_loaded(__FILE__)

end

end

But this doesn’t function. Help is very welcome.

Could you edit the page and fix the ‘code’ formatting - it’s messed up and hard to read/analyze… What are the errors in the Ruby Console ?

I just copy and pasted your ‘mess’ into a new Ruby script, getting rid of smart “.” etc and # out the button icon lines as of course I don’t have those. It worked as expected although I don’t understand Dutch very well ! So what is your problem ?

Thank you for responding. My problem is that it worked once. Every next time I use it, it says it worked, but the description is not added. Could you please share the cleaned script you used? I’m new to Ruby, so not shure what is “mess” and what should stay

I didn’t keep it, sorry. However I didn’t do much except making sure all of the quotes were ordinary ones
" " or ' '
not ‘smart’
“.” or ‘.’
And make sure the images are available for the toolbar buttons.

Thanks. I have a new code (without images) working:

require ‘sketchup.rb’

module DCDescription

def self.set_description
model = Sketchup.active_model
selection = model.selection

if selection.length != 1 || !selection.first.is_a?(Sketchup::ComponentInstance)
  UI.messagebox("Selecteer precies één component.")
  return
end

comp = selection.first
defn = comp.definition

prompts = ["Voer description in:"]
defaults = [defn.description.to_s]
input = UI.inputbox(prompts, defaults, "Component Description")

if input
  defn.description = input[0]
  UI.messagebox("Description opgeslagen.")
end

end

unless file_loaded?(FILE)
cmd = UI::Command.new(“Set Component Description”) {
DCDescription.set_description
}

cmd.small_icon = File.join(__dir__, "icon_16.png")
cmd.large_icon = File.join(__dir__, "icon_24.png")

cmd.tooltip = "Set Component Description"
cmd.status_bar_text = "Pas de description van een component aan"

toolbar = UI::Toolbar.new("Component Tools")
toolbar.add_item(cmd)
toolbar.show

file_loaded(__FILE__)

end

end

Might be filled with “mess” also, since AI generated it for me. If optimization is possible, please let me know

Here’s a how to on properly posting code in the forum:

When you post code on the forum please make sure you use the formatting tool in the bar </> icon, from start to finish - currently you miss out the end and some lines in the middle, so the ‘command’ line has smart-quotes again, the 2-space indents are lost and e.g. _FILE_ auto-formats to bold - that it will break SketchUp when you try and paste it into a Ruby file… ryan beat me to it…