Issue creating macro to add labels after every song, Audacity freezes/crashes

Hello, I’m trying to make a Macro/Script that adds a label at the end of every song imported into Audacity. The purpose of it is to use the exported labels with another program, Label2Cue, which takes the label information and turns them into a readable cue file/sheet.

The standard Audacity macro system doesn’t support things like variables, so you unfortunately can’t tell it to repeat it’s self til all the tracks have labels, and repeating the code 30 times just adds unnecessary tracks at the end of the final track. I tried to make a nyquist macro, but after hours of reading the manual and references in the audacity manual, I’ve hit a roadblock and I’m not sure how to fix it.

My issue now is when running the script, Audacity will hang, and Windows thinks it isn’t responding anymore. I’m guessing it’s stuck in a loop, but I’m not exactly sure why. I don’t know if it’s because my variables aren’t increasing, the tag/goto commands aren’t working correctly, or if “max” isn’t being defined correctly. I’ve tried different commands to fix the issue, but not luck.

Could anyone help me out and point out the error?

Purpose of the code listed: Import all songs then execute script. Insert number of tracks you would like to be labeled. It then selects all, aligns all tracks end to end, selects the first track, move cursor to end of track, adds a label, add most variables by one and repeats it’s self for every other track until it gets to the number of tracks specified to label.

;nyquist plug-in
;version 4
;type tool
;name "Cue File Prep"
;author "ILC"
;release 1.0
;control max "Number of Tracks" int-text "tracks" 5 1 99

(setf labb 0)
(setf tnam 1)
(setf tnum 0)

(AUD-DO "SelectAll:")
(AUD-DO "Align_EndToEnd:")
(tagbody repeat)
(AUD-DO-COMMAND "selecttracks" :mode "Set" :track tnum :trackcount 1)
(AUD-DO "CursTrackEnd:")
(AUD-DO "AddLabel:")
(AUD-DO-COMMAND "setlabel" :label labb :text tnam)
(if (>= tnum max) then (go end) )
(setf tnum (1+ tnum) )
(setf labb (1+ labb) )
(setf tnam (1+ tnam) )
(if (< tnum max) then (go repeat) )


(tagbody end)

I did some more digging around the forms, and I found out about the dotimes command. After adjusting my code, I was able to get the script to work correctly; no more crashes. Not sure how I didn’t search the Nyquist Reference Manual for “loop”, but oh well. I will admit this isn’t the best code. It would be better if it could count the number of tracks it’s self, rather than having to enter the amount manually, but this is my first time coding and it’ll be a while before I figure that out. Here’s the code if anyone wants to use it or see how I solved the issue:

;nyquist plug-in
;version 4
;type tool
;name "Cue File Prep"
;author "ILC"
;release 1.0
;control max "Number of Tracks" int-text "tracks" 5 1 99

(setf labb 0)
(setf tnam 1)
(setf tnum 0)

(AUD-DO "SelectAll:")
(AUD-DO "Align_EndToEnd:")
(dotimes (i max)
	(AUD-DO-COMMAND "selecttracks" :mode "Set" :track tnum :trackcount 1)
	(AUD-DO "CursTrackStart:")
	(AUD-DO "AddLabel:")
	(AUD-DO-COMMAND "setlabel" :label labb :text tnam)
(setf tnum (1+ tnum) )
(setf labb (1+ labb) )
(setf tnam (1+ tnam) )
)