@mrpasini, thanks so much for sharing. I'll be using this one daily! (Another fine example of the amazing versatility of Keyboard Maestro.)
BTW, with KM 8.0.3+ it seems that a local variable should work, but for some reason with this macro it doesn't. (I confirmed on my system: macOS 12.1; KM 10.0.2.) @peternlewis, what are we missing?