Script for variable padding

scripting
Tags: #<Tag:0x00007f0762cdbc50>

#1

I’m trying to write a script that will give variable padding to the track number of a file based on the total track number, and it’s stumping me right now. Here’s what I’ve got that’s not working (doesn’t output anything at all):

	$pad(%tracknumber%,
		$if($gte(%totaltracks%,100),3,
		$if($gte(%totaltracks%,10),2,
				1)),
				0)

Any ideas? Thanks!


#2

Two things:

  1. Just the script above won’t do anything to your tags, as it does not change any variable. If you put your script in the naming string it will output the padded track number at the position where you place it, but if you actually want to modify the tags themselves you have to set a variable using $set() and put this under Options > Scripting. This works for me:

    $set(tracknumber,$num(%tracknumber%,
        $if($gte(%totaltracks%,100),3,
            $if($gte(%totaltracks%,10),2,1)
        )
    ))
    
  2. I would recomment using $num() instead of $pad() (as I did above). The result will be the same, but $num is made for handling numbers and a bit easier to use for that specific case.

  3. Formatting the tracknumber tag with leading zeroes might or might not give you the desired result depending on the file format and playback software you use. As this is supposed to be a number software might interpret it as such (essentially ignoring leading zeroes) and have it’s own logic for displaying padded or not padded numbers. But this is just a general consideration, you have to try this with your tools to see the results.

UPDATE with details for point 3: For MP4 files your formatting will be ignored, since the field is defined to hold an integer, there is no way to store a formatted number. For ID3 it is just a string, so interpretation is up to the playback software, It might display it as you put it, or it might read it as an integer and do it’s own formatting.


#3

Thanks, I’ll try $num when I get back to it. 1 & 3 aren’t a problem. I probably should have explained that this is inside a longer script, the rest of which does what it’s supposed to do. There’s just a blank spot where the track number would be. It’s all going into a string tag, I leave the real track number tag alone.


#4

I don’t know how Picard writes the content of %tracknumber% back to the ID3tag. According to the official ID3 standard it should be the frame

 TRCK
   The 'Track number/Position in set' frame is a numeric string
   containing the order number of the audio-file on its original
   recording. This MAY be extended with a "/" character and a numeric
   string containing the total number of tracks/elements on the original
   recording. E.g. "4/9".

The point is: What is a “numeric string”? A string filled with numbers? A string including a “/” character to split the number of tracks from the total number of tracks?
Does Picard include the “/” automatically?


#5

:rofl: Well, it turns out it was working just fine (but thanks for the tip on $num, I hadn’t realized that existed). It was just treating the whitespace I put in for readability as part of the string, so it LOOKED like the number wasn’t there (it was at the end of the string). Should I make a ticket about this for 2.0? I’m pretty sure it didn’t work that way before.


#6

The format is as described the track number and the total number of tracks separated by a / (e.g. 5/11 for the fifth track on a 11 track release). For Picard those are two separate tags, so it basically reads and writes something like %tracknumber%/%totaltracks%


#7

What I usually do in cases where the script is more complex is that I set some hidden variable. Hidden variables in Picard are available for scripting, but will not be written as tags. They begin with an underscore.

So in your case I would set something like this in Options > Scripting:

$set(_tracknumber,$trim($num(%tracknumber%,
    $if($gte(%totaltracks%,100),3,
        $if($gte(%totaltracks%,10),2,1)
    )
)))

The $trim takes care of any whitespace that might get added due to formatting. Then you can use %_tracknumber% in your naming script and keep the naming script more readable.


#8

That’s a great idea, thanks