Theoretically speaking, the state could be useful for choosing targets of the script's operations. For example, merge the layers in all collapsed groups; or change the font of all text layers in expanded groups (retaining the fonts in collapsed groups).
I suppose it could be useful to target a script's operation based on the state of any
UI element. While it doesn't seem to be that practical of an approach, due to confusion that could occur on the part of the user, I can see how in some cases, it could be useful. It's certainly common to use, for example: the currently selected pattern, gradient or colors in a script.
But the reason I actually want to know this detail is so that I can write an XCF viewer that would be able to display on a web page not only the rendered image, but the layer stack, channels, and paths for the image.
Why would the state of the UI in GIMP matter to an external XCF viewer?