Script to create an album genre from its various recording genres

A couple of comments…

I think the $if() in the first line may not be necessary because %tracknumber% should always be less than or equal to %totaltracks%.

It appears that the script is being executed twice, which explains the growing list. To ensure that each item is only added once you can create a normal variable when each track is processed and only append the genre if the variable is not set. Something like:

$if(%_processed%,,$set_a(_common_genre,$get_a(_common_genre); %genre%))
$set(_processed,1)

Another, perhaps better, option would be to use the $unique() function to remove any duplicate entries from the list. Something like:

$if($and(%genre%,$not(%_processed%)),
  $setmulti(_temp,$trim($get_a(_common_genre); %genre%, ;))
  $set_a(_common_genre,$unique(%_temp%))
  $set(_temp,)
)
$set(_processed,1)

Note that I left in the %_processed% check to save a few processing cycles if the script was run more than once, although it could be removed and should yield the same result. I also used the $trim() function to get rid of any extra semicolons and spaces at the ends of the list.

Finally, considering how tagging scripts are processed (see the processing order in the Scripts documentation), I would actually separate the aggregating of the genres and updating the %genre% tag into two separate scripts. The first script would be:

$if($and(%genre%,$not(%_processed%)),
  $set_a(_common_genre,$unique($trim($get_a(_common_genre); %genre%, ;),,; ))
)
$set(_processed,1)

I shortened it a bit from the version above by removing the temporary variable %_temp% and feeding the semicolon/space separated list directly to the $unique() function.

The second script (located after the first script in the list of tagging scripts) would be:

$set(genre,$if2($get_a(_common_genre),None))

Hopefully this helps explain what you’re seeing, and how you might be able to get around the duplication.

2 Likes