Help replacing special or forbidden characters

I have cobbled together:upside_down_face: a script that does almost everything I need. However, I would like to replace certain characters to my liking and canā€™t quite get the result Iā€™m looking for. My script is:

$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),

	$left($if2(%albumartistsort%, %artistsort%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%albumartist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30) [%artist%]
	,
	-= $upper($firstalphachar($if2(%albumartistsort%, %artistsort%, #))) =-/
	$left($if2(%albumartistsort%, %artistsort%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%artist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30)
	
)

I would like to replace the following with these:

  • = [x] (or a solid dot)
    ? = with nothing
    / = -
    : = -

Does anyone have any clues to help?

Also, I noticed that occasionally the script names the folder -= # =- instead of -= B =- and I canā€™t figure out how itā€™s getting the #. I am also not sure what the first line does or if itā€™s even necessary.

I just noticed the * was replaced with the dot

This is the perfect job for either $replace or $rreplace; I prefer the second, but I definitely know regular expressions can get scary quickly. Basically, youā€™re going to add one of those two functions (and an open parenthesis) before everything you have, and two parameters after everything, the first being the character or character pattern (depending on the function) in the original string and the second being what you want to replace it with:

$replace(everything,?,)
$rreplace(everything,[/:],-)

Note that each character between the square brackets on the second line will be replaced individually, whereas if multiple characters are given for the first, it will only replace instances where a / is followed immediately by a :.

Your issue with the # is from the first line after the single comma; if neither albumartistsort or artistsort is set it falls back on using a number sign, and moreover if one those variables (albumartistsort if it exists, otherwise artistsort) begins with something other than an alphabetic letter ā€“ an artist named ā€œāˆš5ā€, say, or potentially just one in a non-Latin script ā€“ $firstalphachar will also fall back on using the number sign. To best solve that, assuming you want ā€œāˆš5ā€ to be sorted into ā€œ-= āˆš =-ā€, youā€™ll probably want to replace the call with another $left:

-= $upper($left($if2(%albumartistsort%,%artistsort%,#),1)) =-/

That will still come up with the # if neither are set, so replace the character at the end there with whatever you do want to be the default. Also, Iā€™m a bit wary of the spaces after the commas in the original; I removed them in my line above because theyā€™re not always handled like youā€™d expect them to be in function parameters.

Finally, the first line is a check for the Various Artist entity we use to mark compilations: most artists will use the part after the single comma, but if youā€™re saving, say Island of Circles, it will use the part before. Compared to what youā€™re used to, the decorated-first-character level will be bypassed (youā€™ll have ā€œ-= Z =-ā€ and then ā€œVarious Artistsā€), while the file itself will be saved with "Various Artists - " at the start of the file name (more or less like normal) and the specific artistā€™s name will be added to the end:

Various Artists - Island of Circles - 1.09 - Armageddon [Julian Jones]
1 Like

One more note, everywhere thereā€™s a $if2(%albumartistsort%,%artistsort%), with or without the ,# at the end, youā€™ll probably want to add %albumartist% and %artist%; Picard will usually add the sort strings, but having the fallback just in case will catch a couple edge cases. My order would probably be $if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%,#), and you might find it helpful to add a last-chance constant fallback (nothing surrounded by %) even to the calls that donā€™t already have the ,# ā€“ though youā€™ll probably want to choose something that makes more sense.

1 Like

AWESOME! Thanx. One questionā€¦do I place the $replace( outside the $if($eq( or inside it?

Iā€™ll give this all a try tomorrow and see what happens.

LOLā€¦couldnā€™t wait to try this. Hereā€™s what I have:

$rreplace(
$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%albumartist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30) [%artist%]
	,
	-= $upper($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%,#)),1) =-/
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%artist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30)
	)
,[/:],-)

I now get an error: Wrong number of arguments for $left: Expected 2 - 2, got 1 at position 80, line 7. Thatā€™s the replacement line you suggested. Maybe I misunderstood what you meant there. Was I supposed to ADD that line instead of replace it?

Also, will changing the ā€œ/ā€ this way affect the programā€™s use of it for folder naming convention?

1 Like

Nope, I miscounted parentheses. Sorry! Iā€™ve fixed the line I gave, but basically just move the ,1 one parenthesis to the left ā€“ ),1)) rather than )),1). And, yeah, thatā€™s the easiest way to add the replace; you could put it inside the $if($eq( if you wanted to, but where inside isnā€™t quite as clear.

That is a good question about the slash, though. Probably? You may want to create new variables at the beginning to get around that if it does:

$set(_saveartistsort,$rreplace($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25),[/:],-))

and then just use %_saveartistsort% wherever that sequence appeared. For anything that you only use once, like the line I messed up on, you can put the $rreplace inline rather than extracting the sequence, but separating it out keeps things a bit cleaner. Technically, the $set is never completely necessary, but even beside readability, having the same code twice is just begging for you to update one and forget the other, opening the way for weird bugs.

1 Like

LOLā€“Donā€™t you just hate miscounting parentheses? I know it always drives me crazy!

I guess I need to do something with the variable as the script works excellent exceptā€“now the file is renamed to: E:\Wilfr\My Music-= 1 =ā€“Beatles, The-Help! [1965]-The Beatles - Help! - 1.07 - Ticket to Ride.mp3 instead of E:\Wilfr\My Music-= B =-\Beatles, The\Help! [1965]\The Beatles - Help! - 1.07 - Ticket to Ride.mp3. It looks like it works fine to change the characters though. Just canā€™t do the slash this way.

Hereā€™s what I used to get this far:

$rreplace(
$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%albumartist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30) [%artist%]
	,
	-= $upper($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%,#),)1) =-/
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%artist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30)
	)
,[/:],-)

I think Iā€™m beginning to understand this better. Time to investigate the variable option.

Thanx for all the help so far!

Found the error with -= B =- showing as -= 1 =-ā€¦I moved the parenthesis between the , and the 1 by mistake. DUH!

HAH! I think itā€™s working now (I hope). I added the ā€œ_ā€ character to my last line and it seems to have solved the probllemā€¦AC/DC is now AC-DC and that works for me!

Hereā€™s my final codeā€¦if anyone has any suggestions for improvement please let me know!

$rreplace(

$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%albumartist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30) [%artist%]
	,
	-= $upper($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%,#),1)) =-/
	$left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25)/
	$left(%album%,40) [$left(%date%,4)]/
	%artist% - $left(%album%,30) - $num(%discnumber%,1).$num(%tracknumber%,2) - $left(%title%,30)
	)
,[_?:],-)

Now I just need to figure out how to change an asterisk to [x]. The underscore tick changes them to a hyphen because MB changes them to an underscore before any of the scripting. Maybe someone out there will have a solution.

Thanx again for all the helpā€¦this forum is awesome!

Iā€™ve been working on this myself for clarity and to better handle situations where the file doesnā€™t have any artist, say ā€“ and because I just find scripting fun. Sorry this only comes after youā€™ve spent the time yourself! And, no, Iā€™m not sure how to handle the asterisks differently than default; I would suggest turning off ā€œWindows compatibilityā€, but thatā€™s a Unix special character. Anybody else have any input?

$noop(Strip slashes from various variables)
$set(_saveartist,$rreplace($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25),/,-))
$set(_savealbum,$replace($left(%album%,40)$if(%date%,$if(%album%, )[$left(%date%,4)]),/,-))
$set(_savetitle,$replace($if(%album%,$left(%album%,30) - )$num(%discnumber%,1).$num(%tracknumber%,2)$if(%title%, - $left(%title%,30)),/,-))
$set(_cleanedartist,$replace(%artist%,/,-))
$set(_cleanedalbumartist,$replace(%albumartist%,/,-))

$replace(
$rreplace(
$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),
	$noop("Various artists")
	%_saveartist%/
	%_savealbum%/
	%_cleanedalbumartist% - %_savetitle% [%_cleanedartist%]

	$noop(Unknown artists),$if($not(%_saveartist%),
	Unknown artist/
	%_savealbum%/
	%_savetitle%

	$noop(Everybody else),
	-= $upper($left(%_saveartist%,1)) =-/
	%_saveartist%/
	%_savealbum%/
	%_cleanedartist% - %_savetitle%
	))
,[_:],-)
,?,)

EDIT: Actually, the asterisks raise a question about the slashes: are they also replaced ahead of time to avoid unexpected folder creation? That would definitely make sense. Or is that what you were saying about the underscore?

2 Likes

I know you already found the following thread, just wanted to link to it for others:

4 Likes

I knew that sounded familiar, but I was forgetting exactly where I heard it. Thanks for the refresher!

So, @NightMyst, youā€™ll probably want to take the $replace calls out of the first five lines of my most recent script; whether you put the whole strings in a tagging script verbatim or just do so for the variables and leave the rest of the logic here is up to you.

Great! This looks like it helps clean up the code a lot. Iā€™ll give it a try and see how it works. Besides, doing the work at the same time with you helps clarify stuff in my head. I read somewhere you could put a taggerscript in the Optionsā€¦can you only hae one script?

1 Like

Sounds like youā€™ve fallen prey to a pretty common UI bug. You can definitely have more than one script, but the list of scripts apparently likes to hide itself. Mouse over the left border of the script editing box, and your mouse should turn into something indicating you can drag it. Do so, to the right.

1 Like

AH_HA! Thatā€™s solved! Thanx again.

Just a quick updateā€¦the final script seems to work like a charm. I havenā€™t solved the problem with changing asterisks to [x], but everything else works great.

However, when I try to rename the files and do the look-ups MB comes back with them as [non-album tracks] and saves them as noalbum, noartist. These wer all tagged by MB a couple of years ago and successfully matched the items. After manually tagging them with MB they work fineā€¦ Iā€™m working on this problem now, but if anyone has any ideas I would appreciate their input. If I canā€™t find the answer Iā€™ll start a new thread.

They might originally have only saved the recording ID for whatever reason; I have several standalone recordings in my library (or, rather, family mixtapes that arenā€™t notable enough to put on MB) and they act similarly once I remove the release, release group, and track IDs. By re-associating and resaving your files, youā€™re having Picard add those fields back in properly.

When you say noalbum and noartist, do you mean in the tags, or are those the default folders for the script? If itā€™s the latter, thatā€™s more or less what I expect ā€“ I donā€™t think [non-album tracks] comes with release-level tags, though Iā€™m not entirely sure why itā€™s not falling back on recording artist ā€“ but if itā€™s the former, my best guess is that you had a script adding those values a couple years ago, or a young Picard did so itself. Either way, the manual re-association is probably your best solution.

1 Like

I wish I could remember just how long ago I did these files. Iā€™m thinking you may be right. I have been manually re-associating the files and , as a test, any subsequent look-up finds the right data. Other than that issue I havenā€™t found any other problem with the script as we finalized it. Thanx again!

Iā€™ve been playing around with the script that Woven Tales posted.
I got it to how I like it, but Iā€™d like to replace characters in the album folder. Yes this does this at the moment.

This is what Iā€™d like it to do:
For AC/DC, it replaces it as AC-DC, which is great.
But if I have an album name of Gold: Greatest Hits, then it changes it to Gold- Greatest Hits, but Iā€™d rather have it display as Gold - Greatest Hits.

This is the code I have so far. The only thing that I really changed was _savetitle line.

$noop(Strip slashes from various variables)
$set(_saveartist,$rreplace($left($if2(%albumartistsort%,%albumartist%,%artistsort%,%artist%),25),/,-))
$set(_savealbum,$replace($left(%album%,40)$if(%date%,$if(%album%, )[$left(%date%,4)]),/,-))
$set(_savetitle,$replace($if(%album%,$left(%album%,30) - )$if($gt(%totaldiscs%,1),CD %discnumber%/,)$num(%tracknumber%,2)$if(%title%, - $left(%title%,30)),/,-))
$set(_cleanedartist,$replace(%artist%,/,-))
$set(_cleanedalbumartist,$replace(%albumartist%,/,-))

$replace(
$rreplace(
$if($eq(%musicbrainz_albumartistid%,89ad4ac3-39f7-470e-963a-56509c546377),
	$noop("Various artists")
	%_saveartist%/
	%_savealbum%/
	%_cleanedalbumartist% - %_savetitle% [%_cleanedartist%]

	$noop(Unknown artists),$if($not(%_saveartist%),
	Unknown artist/
	%_savealbum%/
	%_savetitle%

	$noop(Everybody else),
	%_saveartist%/
	%_savealbum%/
	%_cleanedartist% - %_savetitle%
	))
,[_:],-)
,?,)