@terdon I have put 6 apps on my home screen and opened them all in succession. I have repeated that 5 times. The Tasker task with the script should have been triggered in each instance for each app, but, as you can see it only write some of them to file. You see 3 4 3 4 5, where you *should* see 6 6 6 6 6. So there is *something* that is being extremely unreliable.
It's probably not your script. It could be something in Android (Android doesn't supply the right data to dumpsys, as when it does mCurrentFocus=null), or it could be something in Tasker. Any idea what I could try to diagnose or …