End Repeat when reaching end of timeline

Hi Guys, Thanks for add.

I want to basically use AppleScript to automate this one task I regularly do in FCPX. It would save me a bunch of time.

I want to automate it to go through the timeline and make CUTS at the end of video clips. In the program to advance I hit the DOWN ARROW, and to make the cut I hit SHIFT+COMMAND+B

Below is the script I have figured out through online research, and it works, but the problem is it doesn’t stop! Kind of basic, but so is the task, it’s just repetitive and takes a while, this does it way quicker. Just automating keystrokes essentially, but I want it to stop on it’s own once it reaches the end of the timeline (won’t move forward anymore), OR I want to be able to hit Escape, or some key to get it to stop running the script.

Forgive me if this is stupid:

activate application “Final Cut Pro”
tell application “System Events”

	key code 125
	keystroke "b" using {shift down, command down}

end tell

end repeat

Because it won’t stop it causes system to freeze. I wasn’t even able to open the automator or stop the apple script, not even Force Quit was accessible. Ideally I could set it up and walk away so it stops automatically AND makes a noise to alert me. I see we can add Beeps!!

However if that’s not possible then I would at least like to hit the ESC key or something to have it stop running. I can tell once it gets to the end in the program, the same clip will be on screen.

I’ve blown hours trying to figure it out through searches online, and on this forum. I sort of get the idea, like “if _______ then exit repeat”, or “repeat until _____” but I don’t know what to write in the BLANK, and there also seem to be other ways of doing this. Whatever is simplest and does the job. I am hoping to work off the above script to just keep it simple, but again your input is appreciated.

I’m replying to myself.

I added

repeat 200 times

this at least limits the number of times so there is an end. Sometimes it falls short, I realize I can make it repeat MORE times BUT, still curious about another way to write that script. However, not as urgent since this is a good working solution.