Get list of animation clips

I’d like to play clips I have in Animation Component randomly.

Is it possible to generate a list of animations names and then choose one randomly to play (JS)?

I’ve tried a few approaches, currently I have the following:

import System.Collections.Generic;
var patternID : List.<String>;
    
    function Start () {
    for (var state : AnimationState in animation) {
    		patternID.Add(state.name);
    	}
    }
    
    function RandomPatternChoice() {
    	var randNum : int = Random.Range(0,patternID.Count);
    	animation.Play(patternID[randNum], PlayMode.StopAll);
    }

But the Console tells me the Component should be marked as Legacy (not possible, animation clip made in Unity), and the List doesn’t populate with the clips (the FOR works, goes though all the clips).

Or try ‘state.clip.name’ (row 6)