I know this is an old thread
But I had the same problem.
I’ve just finished a very nice way to do this.
It also looks at feedhold so the macro can be held ( handy for toolchangers etc)
If stop is pressed it makes safe any outputs and exits gracefully.
I was reading your other post re dereks tool changer. I had the same problems which the new code overcomes