Sean Harding finally put into words an issue that I've witnessed for a while now on macOS Sonoma.
This is it: "Weird MacOS thing: the window I am working in will sometimes lose focus (without the app losing focus). Suddenly, and without any obvious cause. It will be as if no window has focus. Sometimes happens as often as every few minutes, sometimes doesn't happen for a long time. I can’t figure out any common variable. But it can be very disruptive. Has anyone else seen this? Any ideas?"
I've adjusted the Python script mentioned in the thread to work with Python 3.x that you can get with Homebrew for example.
This is what the output looks like:
> python3.11 find_focus_stealer.py
2024-03-08 17:20:34: iTerm2 [/Applications/iTerm.app]
2024-03-08 17:20:37: Safari [/System/Volumes/Preboot/Cryptexes/App/System/Applications/Safari.app]
2024-03-08 17:25:17: CoreServicesUIAgent [/System/Library/CoreServices/CoreServicesUIAgent.app]
2024-03-08 17:25:51: Safari [/System/Volumes/Preboot/Cryptexes/App/System/Applications/Safari.app]
2024-03-08 17:25:52: iTerm2 [/Applications/iTerm.app]
Thanks. I have it installed, but it's not running in background. So it might be something else for me...
Update: indeed it was Carrot...