@MaxGyver I believe the most time consuming part in your macro is the echo iterations line?
//edit Just cobbled together my version of your macro, which I made bulletproof with brackets (CNC-mode expressions are different)
Also placed G91/G90 outside of the loop. No need to repeat those lines 30 times, I guess?
M596 P1 ; select movement queue number
M595 Q1 P10 ; lengthen movement queue to 10 elements
M564 H0 ; allow movement of axis that have not been homed
G91 ; relative
while {(iterations<=30)}
if {(state.gpOut[2].pwm>0 )}
break
G0 W{iterations} ; progress W-axis
G4 P100
G90 ; absolute