So far as i know, there’s no easy way to do this from a script. You can always click the little disclosure triangles. :]
I also don't think there is a easy way. The frist problem is there is no way for a script to know if the layerSet is open or closed by checking the layerSet properties( neither DOM or Action Manager ).
To open a closed layerSet you can make one of the layers in that set the activeLayer.
To close a layerSet is much more involved. You first would have to get all the layerSet properties. Then check to see if it has a channel or vector mask and if so get their setting and dupe them. Then un-group the layerSet( the layers will all be selected after ungrouping ) and group the selected layers( the new layerSet will be closed ). Then reset all of the layerSet properties and restore the masks if needled. With CS6 you also have to deal with possible layer effect.
I have seen a script on PS-Scripts.com that does some of that but I think it misses some setting like layer locking, layer color, channel restrictions. It also doesn't check for vector masks nor keep the channel mask settings.
Thank you very much for your all helps!