How can I know whether a Touch Bar Group is opened by AppleScript or sth?

I hope to open a Touch Bar Group when it is closed and close it when it is opened automatically. Thanks!