Help replacing special or forbidden characters


#1

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.


#2

I just noticed the * was replaced with the dot


#3

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]

#4

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.


#5

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.


#6

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?


#7

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.


#8

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!


#9

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


#10

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!


#11

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?


#12

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


#13

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.


#14

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?


#15

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.


#16

AH_HA! That’s solved! Thanx again.


#17

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.


#18

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.


#19

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!


#20

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%
	))
,[_:],-)
,?,)