# Ruby Script Help

**URL:** <https://forums.sketchup.com/t/ruby-script-help/181599>\
**Category:** Dynamic Components\
**Tags:** dynamiccomponents\
**Created:** [November 17, 2021, 1:37pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599 "2021-11-17T13:37:59Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 17, 2021, 1:37pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/1 "2021-11-17T13:37:59Z")

</div>

Hi Guys,

I have been getting used to creating dynamic components and with your help, I have found out that it is impossible to change a common attribute on multiple components. At some stage, I intend to learn Ruby but at the moment it is beyond me.

My question is if I have a dynamic component called Month with an attribute Current\_Month, and a lot of dynamic components that have an attribute Month, how difficult would it be to write a script that would update the Month attribute to the value of the Current\_Month attribute and how can I attach the script to the attributr Month?

Thanks.

---

<div class="post-metadata">

**Author:** ![Aerilius](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Aerilius](https://forums.sketchup.com/u/Aerilius)\
**Post date:** [November 17, 2021, 2:58pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/2 "2021-11-17T14:58:09Z")

</div>

If you just want to change **a single time** (arbitrary) attributes for multiple entities, you can use [Attribute Inspector](https://extensions.sketchup.com/extension/2c7d7254-76eb-40af-9897-bc7d16b42fb6/attribute-inspector), select all entities and set the value for “Current\_Month”.

If you want to link the dynamic components so that they set/update to the same attribute value, you could use a Ruby script, but keep in mind that this is technically not good design because:

- Dynamic components that you download from 3D Warehouse only depend on the capabilities of the **Dynamic Components extension** (and the attributes defined in the component).
- Your components would additionally depend on the **presence of your “external” script** , otherwise they won’t work. They only work on the exact same setup as you have. They would only save effort for you.

---

<div class="post-metadata">

**Author:** ![nnijmeijer](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/nnijmeijer/32/66166_2.png) [@nnijmeijer](https://forums.sketchup.com/u/nnijmeijer)\
**Post date:** [November 17, 2021, 3:19pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/3 "2021-11-17T15:19:24Z")

</div>

Here is a Ruby Script that may help you.

The script looks at all component definitions present in your SketchUp Model. If it contains the Attribute Dictionary “dynamic attributes”, and the parameter named “current\_month” , the script will provide this parameter with the specified value in the script.

> **Ruby Script**
>
> ```ruby
> mod = Sketchup.active_model # Open model
> 
> # Dynamic Components Attribute Dictionary
> DC_DICT ||= "dynamic_attributes"
> 
> mod.definitions.each{ |compo_definition|
> if compo_definition.attribute_dictionaries[DC_DICT] != nil
> if compo_definition.attribute_dictionaries[DC_DICT].keys.include?("current_month")
> compo_definition.set_attribute(DC_DICT, 'current_month', "November")
> end
> end
> }
> 
> ```

As @Aerilius has already said, it is useful to use the Attribute Inspector.  
With this extension you can see which key / values ​​the dynamic component consists of.

Also check out the Ruby documentation on the Attribute Dictionaries and the Attribute Dictionary.

> **[Class: Sketchup::AttributeDictionaries](https://ruby.sketchup.com/Sketchup/AttributeDictionaries.html)**
>
> The AttributeDictionaries class is a collection of all of the AttributeDictionary objects that are attached to a given Entity object.

> **[Class: Sketchup::AttributeDictionary](https://ruby.sketchup.com/Sketchup/AttributeDictionary.html)**
>
> The AttributeDictionary class allows you to attach arbitrary collections of attributes to a SketchUp entity.

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 10:13am UTC](https://forums.sketchup.com/t/ruby-script-help/181599/4 "2021-11-18T10:13:03Z")

</div>

Hi Aerilius,

I found that when I select the entities, which have more than one attribute, and update them it changes the values of all of the attributes not just the one that I want to change.

There does not appear to be a way to do this without a ruby script from my limited understanding.

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 10:18am UTC](https://forums.sketchup.com/t/ruby-script-help/181599/5 "2021-11-18T10:18:27Z")

</div>

Hi Nick,

Thank you for your help.  
I couldn’t access the script as the post was deleted.

---

<div class="post-metadata">

**Author:** ![nnijmeijer](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/nnijmeijer/32/66166_2.png) [@nnijmeijer](https://forums.sketchup.com/u/nnijmeijer)\
**Post date:** [November 18, 2021, 10:50am UTC](https://forums.sketchup.com/t/ruby-script-help/181599/6 "2021-11-18T10:50:35Z")

</div>

Sorry, something went wrong. But now the post is visible again. 😃

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 11:57am UTC](https://forums.sketchup.com/t/ruby-script-help/181599/7 "2021-11-18T11:57:18Z")

</div>

Nick you are a star. Thank you very much. How do I run a ruby script from within Sketchup?

---

<div class="post-metadata">

**Author:** ![nnijmeijer](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/nnijmeijer/32/66166_2.png) [@nnijmeijer](https://forums.sketchup.com/u/nnijmeijer)\
**Post date:** [November 18, 2021, 1:43pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/8 "2021-11-18T13:43:53Z")

</div>

I often use the “Ruby Code Editor” extension from Alex Schreyer.

You can download this extension from the Extension Warehouse.  
[https://extensions.sketchup.com/extension/07d36510-4de5-49c5-ba63-5cf254c98b2b/ruby-code-editor](https://extensions.sketchup.com/extension/07d36510-4de5-49c5-ba63-5cf254c98b2b/ruby-code-editor)

But you can also run Ruby code using SketchUp Ruby Console. You can access this console via the Window menu.

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 3:53pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/9 "2021-11-18T15:53:17Z")

</div>

Thanks Nick

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 4:24pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/10 "2021-11-18T16:24:02Z")

</div>

Hi Nick,

I got an error and I have no idea what it means

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/e/3/e341d4bb3e4d7512e0eb3a16c526ef576e70f831.jpeg)

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [November 18, 2021, 4:56pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/11 "2021-11-18T16:56:42Z")

</div>

Try seeing of most dynamic attributes are associated with component-instances rather than their component-definition ??

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 6:52pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/12 "2021-11-18T18:52:00Z")

</div>

Hi Tig,

Thank you but I have no idea what that means.

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [November 18, 2021, 7:19pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/13 "2021-11-18T19:19:25Z")

</div>

You posted some code which examined component-definitions [unfortunately not the text but a screen shot !]

If that were adjusted to inspect the component’s instances it will give a different result.  
Here’s some ‘cod-code’ to add in and try…

```auto
...
mod.definitions.each{|defn|
  next if defn.group? ### ? or can groups have DC attribs ??
  next if defn.image?
  defn.instances.each{|inst|
    if inst.attribute_dictionaries && inst.attribute_dictionaries[DC_DICT] && inst.attribute_dictionaries[DC_DICT].keys.include?(" B_CURRENT_MONTH")
      inst.set_attribute(DC_DICT, " B_CURRENT_MONTH", "Jan")
    end
  }
}
...

```

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 7:22pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/14 "2021-11-18T19:22:40Z")

</div>

Thank you for doing this Tig. Where does the code need to be inserted? I have attached the file of code.[MONTH\_UPDATE.rb](https://forums.sketchup.com/uploads/short-url/cmrPud54PW69OBBc8s3wqFNVC2G.rb) (556 Bytes)

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [November 18, 2021, 7:26pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/15 "2021-11-18T19:26:28Z")

</div>

Replace all of your code in the

```auto
mod.definitions.each{|...|
...
}

```

block of code…

I’m happy to help… but I’m not re-writing your code for you - albeit short…

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 18, 2021, 11:51pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/16 "2021-11-18T23:51:29Z")

</div>

Thank you Tig. I really appreciate your help.

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 19, 2021, 5:27pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/17 "2021-11-19T17:27:09Z")

</div>

Hi Tig,

I updated the script - I am not a Ruby script coder sorry- and it complained that defn.is\_group? method doesn’t exist so I commented it out. The script ran but didn’t update the attribute.

 ![image](https://global.discourse-cdn.com/sketchup/original/3X/8/3/8337523825b92f50948390a12a5d33794800afc0.jpeg)  
[MONTH\_UPDATE4.rb](https://forums.sketchup.com/uploads/short-url/u77sTQbrZOn33tcDH50PSEfXyGO.rb) (629 Bytes)

---

<div class="post-metadata">

**Author:** ![nnijmeijer](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/nnijmeijer/32/66166_2.png) [@nnijmeijer](https://forums.sketchup.com/u/nnijmeijer)\
**Post date:** [November 19, 2021, 6:44pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/18 "2021-11-19T18:44:55Z")

</div>

The script I recently shared looped through the component definitions.

But there is also another way to automatically provide the components with the correct data.  
First select the components whose values ​​you want to adjust, and then run the Ruby script below.  
This will then provide the selected component instances with the correct value if it contains the correct Dynamic Attribute.

> **Ruby script**
>
> ```ruby
> mod = Sketchup.active_model # Open model
> sel = mod.selection # Current selection
> 
> # Dynamic Components Attribute Dictionary
> dc_dict = "dynamic_attributes"
> # Attibute Dictionary Key to fill
> key = "current_month"
> # New Attribute Dictionary Key value
> key_value = "November"
> 
> sel.grep(Sketchup::ComponentInstance).each do |compo_instance|
> if compo_instance.attribute_dictionaries && compo_instance.attribute_dictionaries[dc_dict] && compo_instance.attribute_dictionaries[dc_dict].keys.include?(key)
> compo_instance.set_attribute(dc_dict, key, key_value)
> end
> end
> 
> ```

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [November 19, 2021, 7:28pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/19 "2021-11-19T19:28:26Z")

</div>

Sorry about that ! 😳  
I’ve correct my cod-code back in the earlier post.  
It should be `defn.group?` etc - but I mistyped it as `.is_group?`  
You need to do the same fix for `.image?` too…

---

<div class="post-metadata">

**Author:** ![Grey](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/grey/32/92262_2.png) [@Grey](https://forums.sketchup.com/u/Grey)\
**Post date:** [November 19, 2021, 11:24pm UTC](https://forums.sketchup.com/t/ruby-script-help/181599/20 "2021-11-19T23:24:23Z")

</div>

Thanks Tig

[Next page](https://forums.sketchup.com/t/ruby-script-help/181599.md?page=2)
