Script to open a folder?

I'd like to have the possibility that a certain Touch Bar Group would open up as soon as a certain App is in the front. I know I could trigger this group with an App specific action, but for several reasons in my preset I cannot/ don't want to do this.

Is there any possibility to script sth like

  1. if used app is (attention, used, not just open!)
  2. then open Touch Bar group XY?

Thanks for your help guys!