It is not possible to do this with scripts (I have studied this before). There simply is no collaps (or equivalent) layerSets property to set!
However, what you can do is press CTRL (or a command on Mac) and click the small arrow next to the group (in the layers window), which will crash all the groups for you.
There is a solution to work with : but you have to look for it
source share