Triggering this code from a timer actually seems to work (say an interval of 100 msec), but it needs your finger to be on the touchpad (I guess the other scenario works too, but is hardly detectable when you're actually tapping the pad).
There's an app which does it without needing to have your finger there, it's called "Haptic Touch Bar".
Any thoughts how that might be possible?