Stop repeating at the ned of timeline (Protools)

Anyone know of a clever way to get a script to stop repeating at when a playhead or moving timeline indicator reaches the end of the timeline? I’m using protools 12 and I can’t find it for anything. I set up a script to check the muted state of each clip as it moves forward. I basically need it to break from the loop when there are no more clips to check.

Thanks guys, anything would help at this point

On an Imac OS 10.12
Protools 12