"if" in path format - I don't get the syntax

Hello,

there is a “%if{condition,text}” function in beets (https://beets.readthedocs.io/en/stable/reference/pathformat.html), but I can’t work out the correct syntax.

What I want to get: I want to not have the “albumtype” in the new path if it is “album”, but I want to have " ($albumtype)" in the path if it is “ep” or “single”. Like this:
~/music/Beatles, The/Please Please Me/
~/music/Beatles, The/Twist and Shout (ep)/
(“Please Please me” is an album, “Twist and Shout” is an EP)

I started with EPs only to keep the syntax simple:

paths:
    default: %the{$albumartist}/$album%if{albumtype:ep, ($albumtype)}/$track - $title

… but that way I also get the albumtype for albums, not only for EPs:
~/music/Beatles, The/Please Please Me (album)
~/music/Beatles, The/Twist and Shout (ep)

The same with:

    default: %the{$albumartist}/$album%if{albumtype=ep, ($albumtype)}/$track - $title
    default: %the{$albumartist}/$album%if{albumtype==ep, ($albumtype)}/$track - $title
    default: %the{$albumartist}/$album%if{albumtype:'ep', ($albumtype)}/$track - $title
    default: %the{$albumartist}/$album%if{albumtype='ep', ($albumtype)}/$track - $title
    default: %the{$albumartist}/$album%if{albumtype=='ep', ($albumtype)}/$track - $title

What do I have to do to get the albumtype in my path only for EPs and Singles?

Thanks for your help!

Carl

This is admittedly not intuitive, but the docs do have a definition:

  • %if{condition,text} or %if{condition,truetext,falsetext}: If condition is nonempty (or nonzero, if it’s a number), then returns the second argument. Otherwise, returns the third argument if specified (or nothing if falsetext is left off).

That means that condition is not an expression, a beets query, or anything else smart. It’s just text. So the only useful thing you can put in there is, like, a field reference or another template function that expands to something empty/nonempty (or zero/nonzero). For example, %if{$foo,hello,goodbye} expands to hello if the foo field is nonempty and goodbye otherwise.

If you want to do some fancy logic for the condition, people often combine this with the inline plugin to define a useful “condition field” that they then use in an %if.

Ah, I see. The following did what I wanted to get:

plugins: inline the

import:
    copy: yes
    write: yes

paths:
    default: %the{$albumartist}/$album$printalbumtype/$track - $title

item_fields:
    printalbumtype: u' (EP)' if albumtype == 'ep' else u' (Single)' if albumtype == 'single' else u''

Thank you!

Carl

1 Like