$if function question regarding execution section

I would like to know if the $if function supports the scripting as below:

Per the docs, the $if function is as follows
$if(condition,then[,else])

so my curiosity is

$if(%title%,
	$set(%title%,$title($lower(%title%)%_spacer%)
	, $noop(else)
	$set(%title%,%_deftitle%%_spacer%)
)

Essentially what I’m after is the ability to execute blocks of code in the “then” and “else” parts of the $if function.

Would I need to code something like this…

$if(%title%,
{
	$set(%title%,$title($lower(%title%)%_spacer%)
}
	, $noop(else)
{
	$set(%title%,%_deftitle%%_spacer%)
)

Would something like this work currently:

$if($not(%artist%),
	$noop(Check for missing "artist" value as well.)
	$if($not(%artist%),
           $set(%artist%,%_DefSnglArtist%)
           $delete(%artists%)),
		$noop(If there is a value in the "artists" field, then set the "artist" field to the first artist listed in the multi-value "artists")
		$if(is_multi(%artists%),$cleanmulti(artists),
		     $set(%artist%,$title($getmulti(%artists,0)))
        )
	)
)

I don’t know. Is it possible to use blocks of code in each of those sections? If not, would it mean I have to do multiple $if 's one after another to execute each line of code I would like in a “code block”

Yes, you can execute blocks of code, and the format is as in your first example. You can execute as many functions as you like in each of the sections. Just make sure that the functions are not comma-separated in any of the sections because that would trigger a new section. For example:

$if(%test_condition%,
  $noop( True execution )
  $set(foo,a)
  $set(bar,b)
  $noop( Add any other function statements )
,
  $noop( False execution )
  $set(foo,c)
  $set(bar,d)
  $noop( Add any other function statements )
)  

Also, when using the $set() function you generally don’t want the first argument in percent signs (unless you’re doing indirect assignment – assigning to a variable whose name is contained within another variable).

2 Likes

Thank you.

As an extra question, can we define “functions” as used in other languages?

If I understand your question, you can, but it has to be done within a plugin. Otherwise if you’re just repeating the same series of existing functions each time on multiple inputs, you might be able to use one of the Loop functions to avoid a lot of repetition.

2 Likes

Great info and clarification. Time to fix the code.

As an example of using the looping, let’s assume that you want to apply $title() and $swapprefix() to %album%, %title%, %artist% and %albumartist% you could do something like the following (Warning: Untested Code):

$noop( Create list of tags to process )
$setmulti(_list,album; title; artist; albumartist)

$noop( Process each tag using a loop )
$foreach(%_list%,
  $set(%_loop_value%,$swapprefix($title($get(%_loop_value%))))
)

Note that this uses the indirect assignment trick that I mentioned earlier to get the actual tag name to $get() or $set() from a variable.

2 Likes