How Set Date to Minimum of _recording_firstreleasedate and originaldate?

I’d like the Date to be set to the minimum of the years represented in %_recording_firstreleasedate% and %originaldate%.

So far I’ve been using this script, which I understand sets it to %_recording_firstreleasedate%, if present, else %originaldate% if present else leaves it at its current value.
$set(date,$if2(%_recording_firstreleasedate%,%originaldate%,%date%))
…which is not what I’m after

I can’t see any scripting function for arithmetic minimum. But I can see there are functions for numeric comparison e.g. $gt, $lt. So I (naively) guessed I’d need to do something like:
$set(date,if($lt(%_recording_firstreleasedate%,%originaldate%),%_recording_firstreleasedate%,%originaldate%) – which gets error: "1:14 Unexpected character ‘(’.
I see that is the character after the “$lt”. I am confused why that is an error.

Also I wonder what $gt would do (in my script) if one or both of the date-related variables was empty. Would a complete script have to test for empty first before applying comparison functions?

I’m relatively new to scripting in Picard, struggling, but having a go. Any tips (hopefully to avoid fruitless experiment/scouting journeys) would be welcome. Is there a good interactive playground for such functions? Like in foobar or some other app?

And of course, is there any existing solution!

About the script error: there is a missing $ in front of the if.

And yes, if a value is empty $lt would consider it smaller. But maybe combine it with an $if2, which takes the first non empty value:

$set(date,$if2($if($lt(%_recording_firstreleasedate%,%originaldate%),%_recording_firstreleasedate%,%originaldate%),%_recording_firstreleasedate%,%originaldate%,%date%))

I hope I got this right, did not test it and wrote it on my mobile :wink:

But this is supposed to use your condition of the smaller value, but if that turns out to be empty falls back to the first non-empty value.

2 Likes

When would originaldate be less than _recording_firstreleasedate? Why not just use _recording_firstreleasedate?

1 Like

Yes that works great! And it’s good to be certain that’s the logical way to go about this kind of conditional in Picard. Amazing that you could just knock it together so quickly on a phone.

To fully clarify how your script worked, I tab-structured and bracket-coloured it (in a /code block inside Notion), as shown (in the screenshot image) below. I appreciate that Picard-script is not Bash-script, but the auto-colouring worked pretty well. Maybe helpful to others also.

1 Like

It’s all in a state of flux, as I learn and experiment.

Just now for example, I reshaped outsidecontext’s script to:
$set(date,$if2($if($lt(%firstrelyear%,%originalyear%),%firstrelyear%,%originalyear%),%originalyear%,%originalyear%,%date%))

…because in one track for example, the tag specifies:

  • firstrelyear: 1998 – which relates to the compilation album it came from
  • Original Year: 1967 – which is the year I want in Date

firstrelyear itself is assigned by the following script:
$if($gt($left(%_recording_firstreleasedate%,4),$get(origyear)),$set(firstrelyear,%origyear%),$set(firstrelyear,$left(%_recording_firstreleasedate%,4)))

2 Likes

That notation is good to visualize, but be aware that the tagger script language unfortunately is very whitespace sensitive, and all the spaces and linebreaks are part of the input, so the script likely won’t work if you enter it like this in Picard.

There are also tagger script plugins for some text editors, e.g. I have some for vscode and atom. Maybe that’s also something that can help when working with some more complex scripts :slight_smile:

2 Likes

could be for different edits of a track. for example, Speak to Me / Breathe in the Air was combined into a single track on CDs after around 1983, even though The Dark Side of the Moon came out in 1973, (where the two tracks are seperate). I tried listening to my library in order of _recording_firstreleasedate, and that wreaked havoc on that album in particular.


on that note, I may use this script too… :wink:

2 Likes

I wouldn’t recommend doing this as a script. You’ll end up with wrong data for bonus tracks.

Although I guess this is actually what record companies do with the data they give Apple/Spotify, which I have needed to fix because it’s a lie.

Not exactly, because $lt only works if the arguments are integers. The dates (YYYY-MM-DD) in the arguments will cause $lt to always return “” (False) as would a blank argument. To properly compare the full dates, you would need to compare the year, month and date components individually.

In the revised @davidesp script, the comparison works because they are using just the year portion of the date.

2 Likes

You could also remove the dashes and compare “YYYYMMDD” as a number, no? Since it isn’t using weird US nomenclature of MM-DD-YYYY it should still sort and $lt/$gt compare properly, I think.

5 Likes

Yes, but only if you have complete dates. If day or month and day are missing this need to be handled (e.g. by setting it to “00”). So a bit more complicated to do than just replacing the dashes :wink:

2 Likes