Foobar2000:Titleformat Examples: Difference between revisions

From Hydrogenaudio Knowledgebase
(→‎Longer examples: Added contains function example)
(→‎Test whether %tag% field contains string XYZ: added a method for handling empty or missing tags)
Line 105: Line 105:
* With a track that has genre set to ''classic rock'', the output would be ''genre includes rock''.
* With a track that has genre set to ''classic rock'', the output would be ''genre includes rock''.
* With a track that has genre set to ''electronic dance'', the output would be ''genre does not include rock''.
* With a track that has genre set to ''electronic dance'', the output would be ''genre does not include rock''.
Note that this will also return the false value if the field is blank or not present, the following can provide a third output instead:
$puts(string,XYZ)
$puts(tag,[%tag%])
$if($get(tag),$ifgreater($strstr($get(tag),$get(string)),0,tag contains string,tag does not contain string),tag is missing or empty)

Revision as of 22:44, 2 May 2012

Foobar2000_Logo.png

These are some examples of general title format usage contributed by users. They may be of utility to others using Masstagger or other components. Feel free to sign up and contribute your own examples!

Please make sure that you rejoin scripts that have been split into multiple lines before pasting them into the masstagger window. The reason that they are split is to keep this page's formatting readable in your browser.

Shorter examples

Return corresponding tag field

%tag%
  • Example: If artist field is blah, %artist% will return blah.
  • Note: You can insert any character (or space) in between two tags, in front or after; so, if %artist% is blah and %title% is bleh: %artist%X%title% returns blahXbleh.

Trunctuate %tag% to a length of X characters

$cut(%tag%,X)
  • Example: If artist field is blah, $cut(%artist%,3) returns bla

Truncate %tag% by X characters, and add ... at the end of the cut

$cut(%tag%,X)...
  • Example: If artist field is blah, $cut(%artist%,3)... returns bla...

Return numerical value of %tag%, and pad with 0s to X characters

$num(%tag%,X)
  • Example: If %tracknumber% field is 3, $num(%tracknumber%,2) will return 03. If 13, it will return 13.

Return first two characters of a filename, and display as number (if present) padded with 0s to X characters

$num($left(%_filename%),2)
  • Example: A filename of 03_artist_title.mpc will return 03.

Find first occurrence of character X in %tag%, and return everything in front of X

$puts(spacer,$strchr(%tag%,X))
$trim($left(%tag%,$sub($get(spacer),1)))
  • Example: blah X bleh or blah X bleh X bluh returns blah (ie.: artist_album_title -----> title, in case X is _ )

Longer examples

In the next few strings, I have decided to make it possible to define the %tag% that should be edited and the character or string that is used to determine where to trunctuate; these can be defined as variables in the beginning of the string as $puts(tag,%tag%), $puts(char,X), etc. Basically the reason I have done this is to make it simpler to edit these strings, to what you want them to do at that exact moment, in the rather narrow masstager input field. That way it is possible to enter the needed tags and characters only once in the beginning of the line. Again, simply replace %tag% and X by the desired values. Also all of these strings will trim off the trailing and leading spaces of the output (in case there are any).

Find first occurence of character X in %tag%, and return everything in front of X

$puts(char,X)$puts(tag,%tag%)$puts(spacer,$strchr($get(tag),$get(char)))
$trim($left($get(tag),$sub($get(spacer),1)))
  • Example: blah X bleh or blah X bleh X bluh returns blah (ie.: artist_album_title -----> title, in case X is _ )
  • Note: this does exactly the same as the last string mentioned above, except for the introdution of the two variables in the front, meant for easy editing (see explanation above).

Find last occurence of character X in %tag%, and return everything in front of X

$puts(char,X)$puts(tag,%tag%)$puts(spacer,$strrchr($get(tag),$get(char)))
$trim($left($get(tag),$sub($get(spacer),1)))
  • Example: blah X bleh returns blah, and blah X bleh X bluh returns blah X bleh

Find first occurence of character X in %tag% field, and return everything after X

$puts(char,X)$puts(tag,%tag%)$puts(spacer,$strchr($get(tag),$get(char)))
$trim($right($get(tag),
$sub($len($get(tag)),$get(spacer))))
  • Example: blah X bleh returns bleh, and blah X bleh X bluh returns bleh X bluh

Find last occurence of character X in %tag% field, and return everything after X

$puts(char,X)$puts(tag,%tag%)$puts(spacer,$strrchr($get(tag),$get(char)))
$trim($right($get(tag),
$sub($len($get(tag)),$get(spacer))))
  • Example: blah X bleh returns bleh, and blah X bleh X bluh returns bluh

Find first occurence of character X and last occurence of character Y, and return everything in-between

$puts(char1,X)$puts(char2,Y)$puts(tag,%tag%)
$puts(spacer1,$strchr($get(tag),$get(char1)))
$puts(spacer2,$strrchr($get(tag),$get(char2)))
$trim($substr($get(tag),$add($get(spacer1),1),$sub($get(spacer2),1)))
  • Example: blah X bleh Y bluh returns bleh
  • Note: In most cases, X will probably be equal to y here.

Find first occurence of string XYZ, and return everything in front of string XYZ

$puts(string,XYZ)$puts(tag,%tag%)$puts(spacer,$strstr($get(tag),
$get(string)))
$trim($left($get(tag),$sub($get(spacer),1)))
  • Example: blah XYZ bleh returns blah
  • Note: String XYZ can basically be any combination of characters or words.


Find first occurence of string XYZ in %tag% field, and return everything after XYZ:

$puts(string,XYZ)$puts(tag,%tag%)
$puts(spacer,$strstr($get(tag),$get(string)))
$trim($right($get(tag),$sub($len($get(tag)),
$add($get(spacer),$len($get(string))))))
  • Example: blah XYZ bleh returns bleh

Get everything between two strings of your choice

Assuming a text accessible via %someTag% contains the strings <coolTag> and <blaTag>, in that order, you can get everything between them (e.g. xyz from <coolTag>xyz<blaTag>) using this code:

$substr(%someTag%,$add($strstr(%someTag%,<coolTag>),$len(<coolTag>)),$sub($strstr(%someTag%,<blaTag>),1))

What does it do, and how?

  • $substr(%someTag%,X,Y) yields everything of %someTag% from the Xth character to the Yth character. In this case we have:
  • X = $add($strstr(%someTag%,<coolTag>),$len(<coolTag>)),
i.e. the position of <coolTag> in %someTag% plus the length of the word <coolTag>
  • Y = $sub($strstr(%someTag%,<blaTag>),1)
i.e. the position of <blaTag> in %someTag% minus one; the subtraction is performed because otherwise the code would yield < (the first character of <blaTag>) as the last character of the output string.

Test whether %tag% field contains string XYZ

$puts(string,XYZ)
$puts(tag,%tag%)
$ifgreater($strstr($get(tag),$get(string)),0,tag contains string,tag does not contain string)

An example of how this could be used:

$puts(string,rock)
$puts(tag,%genre%)
$ifgreater($strstr($get(tag),$get(string)),0,genre includes $get(string),genre does not include $get(string))
  • With a track that has genre set to classic rock, the output would be genre includes rock.
  • With a track that has genre set to electronic dance, the output would be genre does not include rock.

Note that this will also return the false value if the field is blank or not present, the following can provide a third output instead:

$puts(string,XYZ)
$puts(tag,[%tag%])
$if($get(tag),$ifgreater($strstr($get(tag),$get(string)),0,tag contains string,tag does not contain string),tag is missing or empty)