Hi @peternlewis. Please consider adding a Keyboard Maestro trigger for Desktop Space changes.
If I'm correctly reading the following entry in stack overflow, it appears there is a programatic method:
objective c - Detecting when a space changes in Spaces in Mac OS X - Stack Overflow
If you were to add that trigger, I'd be able to simplify and improve several macros that I use including a set that I've shared on the forum: Desktop Spaces • Macros to Improve Navigation and Window Management
Absent that trigger, in macros that need to save the Desktop Space, I've used the focused window changes trigger to query and save the space number. That trigger does not always change when the Desktop Space changes and obviously triggers many times when the space does not change.
On a related note, the tiling window manager yabai does detect Desktop Space changes. I've configured yabai to run a Keyboard Maestro macro that queries and saves the Desktop Space number to a Keyboard Maestro dictionary value. It's ugly, but it does work reliably.
Thanks for considering my request.