<p>I ended up making a custom function that gets the number of values in a dropdown effect. I have no idea if this is a good way of doing it but it worked for me! :)</p>
<pre><code>function getDropdownLength(dropdownMenu) {
    var length = 1;
    var originalValue = dropdownMenu.value;
    dropdownMenu.setValue(1);

    while (true) {
        try {
            dropdownMenu.setValue(dropdownMenu.value + 1);
            length++;
        }
        catch (e) {
            break;
        }
    }

    dropdownMenu.setValue(originalValue);
    return length;
}
</code></pre>

CC BY-SA 4.0
2023-09-01T14:25:57.567Z
2023-09-01T14:25:57.567Z
klock
<p>I'm trying to create a script that randomly selects an option from a drop-down list.</p>
<p>For that I use a randomly generated number as the index of the set value. The issue is that if it tries to select an item that doesn't exist (like, if the randomly generated number is 6 and the list only has 5 options), the script stops and doesn't run the rest of the code.</p>
<p>So it needs to generate a random integer in the range from 1 to the exact number of options. For now I have hard coded the range in the script, so it works ok in this specific case, but I would like it to work for whatever number of options the drop-down may have, so it continues working if the number of options is changed.</p>
<p>So I would need to get the number of options in the drop-down to set as the maximum value of the range to generate a random number from.
But I can't find anywhere a way to get the &quot;size&quot; or &quot;length&quot; of the drop-down options. How do I do that?</p>

<after-effects><after-effects-scripting>
How to get the number of options in a drop-down list (After Effects script)?
Alexandre Marcati
# How to get the number of options in a drop-down list (After Effects script)?

(Asked by: username_0 on 2023-05-19)

I'm trying to create a script that randomly selects an option from a drop-down list.

For that I use a randomly generated number as the index of the set value. The issue is that if it tries to select an item that doesn't exist (like, if the randomly generated number is 6 and the list only has 5 options), the script stops and doesn't run the rest of the code.

So it needs to generate a random integer in the range from 1 to the exact number of options. For now I have hard coded the range in the script, so it works ok in this specific case, but I would like it to work for whatever number of options the drop-down may have, so it continues working if the number of options is changed.

So I would need to get the number of options in the drop-down to set as the maximum value of the range to generate a random number from.
But I can't find anywhere a way to get the "size" or "length" of the drop-down options. How do I do that?

**Tags:** after-effects, after-effects-scripting


---

## 1 answers

### Answer by username_1 on 2023-09-01. Score: 1 | Accepted answer

I ended up making a custom function that gets the number of values in a dropdown effect. I have no idea if this is a good way of doing it but it worked for me! :)
function getDropdownLength(dropdownMenu) {
    var length = 1;
    var originalValue = dropdownMenu.value;
    dropdownMenu.setValue(1);

    while (true) {
        try {
            dropdownMenu.setValue(dropdownMenu.value + 1);
            length++;
        }
        catch (e) {
            break;
        }
    }

    dropdownMenu.setValue(originalValue);
    return length;
}


---

