Switching Focus After Last Window Close

One of the minor bugaboos I have with macOS is that when you close the last window of an application, it is still the focus application (name and menu items across the top of the screen in the menu bar). I would expect it to refocus the last active application window (as cmd+tab would do).

Is there a way to get this behaviour on my system using BTT or something else?

I've tried a few things in BTT like intercepting cmd+w and then forwarding cmd+w to the application, then with the intention to write an apple script after which is test for open windows and switch focus accordingly. That method seems to cause BTT to crash (I guess it infinite loops).

I also had a look at triggers, though there doesn't seem to be any for window closing.

Got to the stage where I should probably ask for suggestions as there may be a clean way to do it I'm not aware of.

Thanks