I’m trying to create a macro to pad all the track numbers in the Track Names with 0 (track 9 to track 09)
Here is what i have:
(let ((tracks (aud-get-info "tracks")))
(dotimes (j (length tracks))
(setf track (nth j tracks))
(setf TrackName (second (assoc 'name track)))
(setf NewTrackName "")
; Need to split TrackName on " " into list my-list
(dolist (str my-list)
; Need to check if str is a number
; If it is format it as a zero padded string of 2 characters ie 09 into fStr
; Else set fStr = str
(setf tempTrackName (format nil "~a ~a" NewTrackName fStr))
(setf NewTrackName tempTrackName))
(aud-do-command "SelectTracks" :track j :trackcount 1)
(aud-do-command "SetTrackStatus:" :name (format nil "~a" NewTrackName))))
Thank you for you help trouble with the Testing for a number, I suspect the issue is with my if statement.
Can someone please point out what I’m doing wrong?
For example, if you want to pad integer numbers to a minimum number of digits, you could do something like:
(defun pad-zeros (str num)
;; Return integer padded with zeros to a minimum of'num' digits.
;; Return value as a string.
(cond ((integerp str) (setf str (format nil "~a" str)))
((not (stringp str))
(error "First argument must be an integer, or integer as a string.")))
(let ((ln (length str)))
(if (>= ln num)
str
(dotimes (i (- num ln) str)
(setf str (strcat "0" str))))))
and then call the function (in this case, padding to 2 digits):
The goal was to zero pad the track numbers so I can sort them correctly before aligning them end to end and mixing them down to a single track. Since I don’t see myself ever working with 100 Plus tracks I never need to deal with 3 or more positions. Some track names have numbers other than the track numbers, if they are 1 digit numbers the zero padding doesn’t heart anything but if they are longer I didn’t want them getting truncated to 2 digits so I added the if (< (length str) 2) condition to ignore anything longer than 1 digit.
Maybe also worth commenting that aud-do-command "SetTrackStatus:" strips leading spaces in the new name. This seems to be an undocumented “feature”, so it shouldn’t really be relied on. If it were not for this feature, the above code would add a space at the start of each track name.
Here’s an alternative solution.
It’s a bit longer, mostly because it does not rely on (aud-do-command "SetTrackStatus:") to strip the space before the first word (see (join-words ...)).
A feature in this code that I thought you might find interesting is the line:
This makes an input stream from the txt string, then reads the stream back as a list (of characters). We can then iterate through the list rather than reading the string character by character.