MusicBrainz Picard/Documentation/Scripting

From MusicBrainz Wiki
< MusicBrainz Picard‎ | Documentation
Revision as of 20:21, 20 January 2012 by JacksonATL (talk | contribs) (Add script for organizing music collection alphabetically)
Jump to navigationJump to search

This page describes the simple scripting language implemented in MusicBrainz Picard.

Syntax

The syntax is derived from Foobar2000's titleformat. There are three base elements: text, variable and function. Variables consist of alpha-numeric characters enclosed in percent signs (e.g. %artist%). Functions start with a dollar sign and end with an argument list enclosed in parentheses (e.g. $lower(...)).

To use parenthesis or commas as-is inside a function call you must escape them with a backslash.

Metadata Variables

See Picard tags for list of usable variables.

Technical Variables

%_extension%

  • Extension of the file name. For example mp3 for file 01 Track.mp3.

Functions

$if(if,then,else)

  • If if is not empty, it returns then, otherwise it returns else.

$if2(a1,a2,a3,...)

  • Returns first non empty argument.

$lower(text)

  • Returns text in lower case.

$upper(text)

  • Returns text in upper case.

$left(text,num)

  • Returns first num characters from text.

$matchedtracks()

Added in version 0.12

  • Returns the number of matched tracks within a release.

$right(text,num)

  • Returns last num characters from text.

$num(num,len)

  • Returns num formatted to len digits.

$replace(text,search,replace)

  • Replaces occurrences of search in text with value of replace and returns the resulting string.

$rsearch(text,pattern)

$rreplace(text,pattern,replace)

$in(x,y)

  • Returns true, if x contains y.

$unset(name)

  • Unsets the variable name.

$set(name, value)

  • Sets the variable name to value.

Note: To create a variable which can be used for the file naming string, but which will not be written as a tag in the file, prefix the variable name with an underscore. %something% will create a "something" tag; %_something% will not.

$setmulti(name, value, separator="; ")

  • Sets the variable name to value, using the separator (or "; " if not passed) to coerce the value back into a proper multi-valued tag. This can be used to operator on multi-valued tags as a string, and then set them back as proper multi-valued tags, e.g
$setmulti(genre,$lower(%genre%))


$get(name)

  • Returns the variable name (equivalent to %name%).

$copy(new,old)

  • Copies metadata from variable old to new. The difference between $set(new,%old%) is that $copy(new,old) copies multi-value variables without flattening them.

$copymerge(new,old)

  • Merges metadata from variable old into new, removing duplicates and appending to the end, so retaining the original ordering. Like <nowiki$copy</nowiki>, this will also copy multi-valued variables without flattening them.

$trim(text[,char])

  • Trims all leading and trailing whitespaces from text. The optional second parameter specifies the character to trim.

$add(x,y)

  • Add y to x.

$sub(x,y)

  • Subtracts y from x.

$div(x,y)

  • Divides x by y.

$mod(x,y)

  • Returns the remainder of x divided by y.

$mul(x,y)

  • Multiplies x by y.

$or(x,y)

  • Returns true, if either x or y not empty.

$and(x,y)

  • Returns true, if both x and y are not empty.

$not(x)

  • Returns true, if x is empty.

$eq(x,y)

  • Returns true, if x equals y.

$ne(x,y)

  • Returns true, if x not equals y.

$lt(x,y)

  • Returns true, if x is lower than y.

$lte(x,y)

  • Returns true, if x is lower than or equals y.

$gt(x,y)

  • Returns true, if x is greater than y.

$gte(x,y)

  • Returns true, if x is greater than or equals y.

$noop(...)

  • Does nothing (useful for comments or disabling a block of code).

$len(text)

  • Returns the number of characters in text.

$performer(pattern="",join=", ")

  • Returns the performers where the performance type (e.g. "vocal") matches pattern, joined by join.

$firstalphachar(text,nonalpha="#")

  • Returns the first character of text. If text is not an alphabetic character nonalpha is returned instead.

$initials(text)

  • Returns the first character of each word in text, if it is an alphabetic character.

$truncate(text,length)

  • Truncate text to length.

Tagger Script Examples

OptionsOptions...AdvancedScripting

Tagger Script usually creates, modifies or deletes metadata variables.

Artist names

$if($search(%album%,(feat. conductor)),
  $set(artist,%orchestra%))
  • Stupid assumption that all classical albums have "feat. conductor" in the title, but it shows the idea. :)

Live tracks on live albums

$if($and($eq(%releasetype%,live),$not($in(%title%,\(live\)))),$set(title,%title% \(live\)))

Remove "feat." from track titles

$set(title,$rreplace(%title%,\\s\\\(feat. [^\)]+\\\),))

Convert triple-dot to ellipsis

$set(title,$replace(%title%,...,…))
$set(album,$replace(%album%,...,…))

This one is useful for people concerned about correct typography and also fixes one problem on Linux: if an album (assuming it's also a directory) is called something like "...for you!", it is considered hidden and therefore might be not accessible from some applications.

File Naming Examples

OptionsOptions...File naming

Lower case filenames with underscores

$lower($replace(%albumartist%/%album%/$num(%tracknumber%,2) %title%, ,_))

(Year) only if available

$if(%date%,\($left(%date%,4)\))

Use a different naming pattern for NATs

$if($eq([non-album tracks],%album%),[non-album tracks]/%tracknumber%. %artist% - ,%artist% - $if(%date%, $left(%date%,4) )- %album%/%tracknumber%.)%title%

Result:

  • Non-Album Tracks: [non-album tracks]/Band Name - Track Name.ext
  • Tracks in releases: Band Name - year - Release Name/##. Track Name.ext

Remove "Various Artists" on compilations

By default, Picard marks various-artist compilations with an album artist called "Various Artists". Even if you don't have anything by Torsten Pröfrock, it still means that you player will sort your comps between Vangelis and VCR instead of down at the end, which you may not want.

This is easy to fix:

$if($and($eq(%compilation%,1), $eq(%albumartist%,Various Artists)), $unset(albumartist) $unset(albumartistsort))

Merge writers into both composer and lyricist tags

Suppose you want to add anyone involved in writing to both the composer and lyricist tags.

$copymerge(composer,writer)
$copymerge(lyricist,writer)
$unset(writer)

Organize by alphabetical folders excluding leading The

To keep music organized by initial letter of artist's name you must first set the directory where saved files are to be stored in Options --> Moving Files. Then under Options --> File Naming check Rename files when saving. This script will then move saved files to your Moving Files location using the following hierarchy: A/Artist Name, The/Year - Album Name/Artist - Album - Track - Song Title

$firstalphachar(
   $if(
	$eq($left($lower(%artist%),4),the ),
		$right($upper(%artist%), $sub($len(%artist%),4)),
	$upper(%artist%)
   ))/

$if(
	$eq($left($lower(%artist%),4),the ),
		$right(%artist%, $sub($len(%artist%),4))\, The,
	%artist%
)/

$if(%date%,$left(%date%,4) - )%album%/

%artist% - %album% - $if($gt(%totaldiscs%,1),Disk %discnumber% - ,)$num(%tracknumber%,2) - %title%