Replacing Special Characters

Hey guys,

I’d like to remove all the special characters (such as , or ‘ or -) from my tags, mainly from title and artist. I tried using $replace but didn’t get anywhere. Would love some input!

I currently wrote a script (via preferences > scripting) that just goes:

$replace(%title%,’,)

Just replacing ‘ with nothing, but for some reason, it doesn’t work. I took a song with a title “Don’t be afraid”, applied the script - no change. I don’t quite know where I’m going wrong here.

Kindly,

Rido

Just $replace does not alter a tag. You have to set the tag with $set, something like:

$set(title,$replace(%title%,’,))
2 Likes

Ahh, that makes a lot of sense! I knew I was missing something obvious…

Is there a way to remove all special characters using replace? Or do I have to do it individually for every special char?

You could try something like the following (untested code):

$setmulti(_tags_to_clean,title; artist; add_as_required)
$set(_characters_to_remove,'")
$foreach(%_tags_to_clean%,
  $set(%_loop_value%,$rreplace($get(%_loop_value%),[%_characters_to_remove%],))
)

Note that you may need to escape some of the characters like (, ), and , in the %_characters_to_remove% variable with a backslash as \(. Some characters like [ and ] may need to be escaped with two backslashes as \\[. The end result of the escaping should be a list of characters suitable for inclusion between square brackets in a regular expression.

Alternately, you could eliminate the %_characters_to_remove% variable entirely and just build the regular expression match string directly as the second argument to the $rreplace() call (with appropriate escaping as required).

1 Like

I tried this for title:

$set(_title,$swapprefix(%title%))
$set(_title,$trim(%_title%))
$set(_title,$replace(%_title%,:,))
$set(_title,$replace(%_title%,/,-))
$set(_title,$replace(%_title%,\\,))
$set(_title,$replace(%_title%,*,))
$set(_title,$replace(%_title%,?,))
$set(_title,$replace(%_title%,",))
$set(_title,$replace(%_title%,>,))
$set(_title,$replace(%_title%,<,))
$set(_title,$replace(%_title%,|,))
$set(_title,$rreplace(%_title%, {2\,}, ))

but in some case seems MBP doesnt follow instructions and at the end I have something like this:

title is:
End of Eternity Balor – The One Room World / Airlock / Malice Aforethought

result is:

01-11 End of Eternity Balor – The One Room World _ Airlock _ Malice Aforethought

note the underscore instead of “-” I set in the rule

Think that @rdswift proposal is very interesting

I assume that you’re seeing this in your file naming. Are you using the %_title% variable that you’re processing, or are you still using the original %title% tag in your naming script?

1 Like

Thanks for answer @rdswift

Yes, I use in my filename composition %_title% and not %title%.

Think that MBP somehow bypass my rules and substitute the forbidden caharacter with its own list… am I wrong?

for a complete info this is my complete script:

$set(_album,%album%)
$set(_album,$trim(%_album%))
$set(_album,$replace(%_album%,:,))
$set(_album,$replace(%_album%,Original Motion Picture Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music From the Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,Original Series Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Television Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Series,\(OST\)))
$set(_album,$replace(%_album%,Original Series Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Sound & Song Track,\(OST\)))
$set(_album,$replace(%_album%,Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music from Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,Music from the Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,The Motion Picture Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music from the Soundtrack,\(OST\)))
$set(_album,$rreplace(%_album%, {2\,}, ))
$set(_album,$rreplace(%_album%,\({2\,},\())
$set(_album,$rreplace(%_album%,\){2\,},\)))

$set(_albumartistname,$swapprefix(%albumartist%))
$set(_albumartistname,$trim(%_albumartistname%))
$set(_albumartistname,$replace(%_albumartistname%,:,))
$set(_albumartistname,$rreplace(%_albumartistname%, {2\,}, ))

$set(_artistname,$swapprefix(%artist%))
$set(_artistname,$trim(%_artistname%))
$set(_artistname,$replace(%_artistname%,:,))
$set(_artistname,$rreplace(%_artistname%, {2\,}, ))

$set(_title,$swapprefix(%title%))
$set(_title,$trim(%_title%))
$set(_title,$replace(%_title%,:,))
$set(_title,$replace(%_title%,/,-))
$set(_title,$replace(%_title%,\\,))
$set(_title,$replace(%_title%,*,))
$set(_title,$replace(%_title%,?,))
$set(_title,$replace(%_title%,",))
$set(_title,$replace(%_title%,>,))
$set(_title,$replace(%_title%,<,))
$set(_title,$replace(%_title%,|,))
$set(_title,$rreplace(%_title%, {2\,}, ))

$set(_year,$left($if2(%date%,%originaldate%,0000),4))

$set(_format,$upper(%_extension%))

$set(_audio,$if2(%_bits_per_sample%,0)-$div($if2(%_sample_rate%,0),1000))

$set(_catno,$if2(%catalognumber%,No_Cat_Nr))
$set(_catno,$replace(%_catno%, ,_))
$set(_catno,$replace(%_catno%,-,_))

$set(_label,$if2(%label%,No_Label))

$set(coverart,%coverart_url%)

$set(_media,$if2(%media%,No_media))
$set(_media,$replace(%_media%,:,_))
$set(_media,$replace(%_media%,",_))
$set(_media,$replace(%_media%, ,_))
$set(_media,$rreplace(%_media%,_{2\,},_))


$set(_disccount,$if($gt(%totaldiscs%,1),%totaldiscs%Discs))

$if($or($inmulti(%_secondaryreleasetype%,soundtrack),
		$inmulti(%_secondaryreleasetype%,Soundtrack),
		$inmulti(%genre%,soundtrack),
		$inmulti(%genre%,Soundtrack),
		$inmulti(%_primaryreleasetype%,soundtrack),
		$inmulti(%_primaryreleasetype%,Soundtrack),
		$in(%_album%,Kuschelrock),
		$in(%_album%,KuschelRock)
		),
    $set(_basepath,$rreplace(%_album% %_disccount% %_media% - %_albumartistname% \(%_year%\) %_format% %_audio% %_catno% %_label%, {2\,}, )),
    $set(_basepath,$rreplace(%_albumartistname% - %_album% %_disccount% %_media% \(%_year%\) %_format% %_audio% %_catno% %_label%, {2\,}, ))
)

$if($gt(%totaldiscs%,1),
$set(_basepath,$if(%discsubtitle%,%_basepath%/Disc $num(%discnumber%,2) %discsubtitle%,%_basepath%/Disc $num(%discnumber%,2))))

$if($gt(%totaldiscs%,1),
    $if($and(%albumartist%,$ne(%albumartist%,%artist%)),
        %_basepath%/$num(%discnumber%,2)-$num(%tracknumber%,2) %_title% - %artist%,
        %_basepath%/$num(%discnumber%,2)-$num(%tracknumber%,2) %_title%
    ),
    $if($and(%albumartist%,$ne(%albumartist%,%artist%)),
        %_basepath%/$num(%tracknumber%,2) %_title% - %artist%,
        %_basepath%/$num(%tracknumber%,2) %_title%
    )
)

Try moving all your replacement stuff out of your naming script into a tagger script. I may be wrong, but I seem to recall something about Picard already “sanitizing” tags provided to the naming script.

maybe this is out of my knowledge;
If I understand correctly you suggest to move every replace command in another script?

Yes.

You would have a tagger script (perhaps called “Make replacements”) as:

$set(_album,%album%)
$set(_album,$trim(%_album%))
$set(_album,$replace(%_album%,:,))
$set(_album,$replace(%_album%,Original Motion Picture Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music From the Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,Original Series Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Television Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Series,\(OST\)))
$set(_album,$replace(%_album%,Original Series Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Original Sound & Song Track,\(OST\)))
$set(_album,$replace(%_album%,Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music from Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,Music from the Motion Picture,\(OST\)))
$set(_album,$replace(%_album%,The Motion Picture Soundtrack,\(OST\)))
$set(_album,$replace(%_album%,Music from the Soundtrack,\(OST\)))
$set(_album,$rreplace(%_album%, {2\,}, ))
$set(_album,$rreplace(%_album%,\({2\,},\())
$set(_album,$rreplace(%_album%,\){2\,},\)))

$set(_albumartistname,$swapprefix(%albumartist%))
$set(_albumartistname,$trim(%_albumartistname%))
$set(_albumartistname,$replace(%_albumartistname%,:,))
$set(_albumartistname,$rreplace(%_albumartistname%, {2\,}, ))

$set(_artistname,$swapprefix(%artist%))
$set(_artistname,$trim(%_artistname%))
$set(_artistname,$replace(%_artistname%,:,))
$set(_artistname,$rreplace(%_artistname%, {2\,}, ))

$set(_title,$swapprefix(%title%))
$set(_title,$trim(%_title%))
$set(_title,$replace(%_title%,:,))
$set(_title,$replace(%_title%,/,-))
$set(_title,$replace(%_title%,\\,))
$set(_title,$replace(%_title%,*,))
$set(_title,$replace(%_title%,?,))
$set(_title,$replace(%_title%,",))
$set(_title,$replace(%_title%,>,))
$set(_title,$replace(%_title%,<,))
$set(_title,$replace(%_title%,|,))
$set(_title,$rreplace(%_title%, {2\,}, ))

$set(_year,$left($if2(%date%,%originaldate%,0000),4))

$set(_format,$upper(%_extension%))

$set(_audio,$if2(%_bits_per_sample%,0)-$div($if2(%_sample_rate%,0),1000))

$set(_catno,$if2(%catalognumber%,No_Cat_Nr))
$set(_catno,$replace(%_catno%, ,_))
$set(_catno,$replace(%_catno%,-,_))

$set(_label,$if2(%label%,No_Label))

$set(coverart,%coverart_url%)

$set(_media,$if2(%media%,No_media))
$set(_media,$replace(%_media%,:,_))
$set(_media,$replace(%_media%,",_))
$set(_media,$replace(%_media%, ,_))
$set(_media,$rreplace(%_media%,_{2\,},_))

And then your naming script would be:

$set(_disccount,$if($gt(%totaldiscs%,1),%totaldiscs%Discs))

$if($or($inmulti(%_secondaryreleasetype%,soundtrack),
		$inmulti(%_secondaryreleasetype%,Soundtrack),
		$inmulti(%genre%,soundtrack),
		$inmulti(%genre%,Soundtrack),
		$inmulti(%_primaryreleasetype%,soundtrack),
		$inmulti(%_primaryreleasetype%,Soundtrack),
		$in(%_album%,Kuschelrock),
		$in(%_album%,KuschelRock)
		),
    $set(_basepath,$rreplace(%_album% %_disccount% %_media% - %_albumartistname% \(%_year%\) %_format% %_audio% %_catno% %_label%, {2\,}, )),
    $set(_basepath,$rreplace(%_albumartistname% - %_album% %_disccount% %_media% \(%_year%\) %_format% %_audio% %_catno% %_label%, {2\,}, ))
)

$if($gt(%totaldiscs%,1),
$set(_basepath,$if(%discsubtitle%,%_basepath%/Disc $num(%discnumber%,2) %discsubtitle%,%_basepath%/Disc $num(%discnumber%,2))))

$if($gt(%totaldiscs%,1),
    $if($and(%albumartist%,$ne(%albumartist%,%artist%)),
        %_basepath%/$num(%discnumber%,2)-$num(%tracknumber%,2) %_title% - %artist%,
        %_basepath%/$num(%discnumber%,2)-$num(%tracknumber%,2) %_title%
    ),
    $if($and(%albumartist%,$ne(%albumartist%,%artist%)),
        %_basepath%/$num(%tracknumber%,2) %_title% - %artist%,
        %_basepath%/$num(%tracknumber%,2) %_title%
    )
)

How much to include in one script versus the other is a bit flexible. Just be sure to include all the replacement stuff in the tagger script so that it is working with the original text rather than the “sanitized” version of the text provided to the naming script.

1 Like

I’m reading FAQ… If I understand correctly:

part 1 tagger script must be placed in:
Options / Scripting (Enable Tagger Script(s))

and part 2 naming script remain as a separate script in Options / Open file naming script editor

right?

Right. The tagger script should will be executed first.

1 Like

You can’t replace directory separators (slash and backslash) in file naming scripts, as they get replaced before executing the script.

Use the “Replace directory separators with” option in Options > File Naming > Compatibility to change the replacement.

2 Likes