INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Tips & Tricks: Expressions for Field Value Manipulation  (Read 1114 times)

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Tips & Tricks: Expressions for Field Value Manipulation
« on: March 14, 2021, 01:08:21 pm »

Expression Field Assignment is very powerful and useful for manipulating the values of your Fields and can do all kinds of clever things.

In a recent thread, I pointed out how =Counter() could be used to automatically "number" any field sequentially, just like the built-in Fill Track Numbers from List Order tool. It dawned on me that many other folks may not be aware of this powerful feature introduced in the Liberace Rubber Chicken Build in the long, long ago. And, more importantly, it would be cool to have a thread where we could share handy expressions to use for manipulating fields! So, come out your pockets!

For the uninitiated:

You can assign the value of any field to the "output" of an expression by prefixing the expression with an equals sign (=) in the field editing box (in the Tag Action Window or inline in a details view). This can be used to "modify and massage" the information included your fields in Media Center in almost any way you can imagine using the Expression Language.

The simplest example is to copy information from one field to another as it is. Say you wanted to copy the value of [Album] over to the [Comment] field on a bunch of files for some reason. You can do this "inline" by:
1. Select a file or set of files.
2. Edit the [Comment] value in the Tag Action Window.
3. Type: =[Album]

That's cool, but of course the built in Library Tools\Move / Copy Fields tool does this and has more options than just copying the existing value. But, the power is that you use any expression to manipulate the values first.

I provided another simple example in the other thread linked above. If you want to automatically "number" any field, just like the built-in Library Tools\Fill Track Numbers from List Order tool does on [Track #] you can:
1. Select the set of files.
2. Edit the field you want to number in the Tag Action Window.
3. Type: =Counter()

When editing a field, you can even manipulate the current values of that field by simply referencing "itself" in Square Bracket Notation. So, if you have a bunch of files that have good [Name] values, but for some reason all have 7 random digits after them in the field (as in my original example requesting the feature) you can use:
Code: [Select]
=RemoveRight([Name],7)
Even when you are editing the [Name] field. It uses the value of the field from before the new expression is applied "to itself". However, see my first tip below...
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #1 on: March 14, 2021, 01:25:13 pm »

Avoiding Disaster When Practicing and Get Yourself a Text File

My first two tips are to:
1. Avoid editing any field "inline" when you are first "developing" an expression
2. Save yourself a text file (or two) where you can save these expressions for future use. Then using them is a copy/paste away!

For safety, anytime I'm doing any kind of manipulation on an existing field that is complicated or that I haven't done a bunch before (and then it'll usually be in my Field Value Expressions text file for copy/pasting), I apply the changes to [Comment] instead of directly to the field where I want the results directly. That way, the existing field's value stays un-modified until I get [Comment] all how I want it, and then I simply copy the value of Comment over to the one I really wanted to change at the end.

I use [Comment] because it is built-in and doesn't (in my Library) usually contain anything useful at all. If you don't want to overwrite that field's value, you can use any field, or make yourself a [Scratch] custom Library Field.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #2 on: March 14, 2021, 01:26:16 pm »

Removing Junk with ListItem()

Another of my favorites is using =ListItem() to trim field values of useless junk. If you have any field that ends up containing both "good" and "bad" information as long as there is some kind of "delimiter" between the good and bad information, you can trim that useless info off lickety-split with =ListItem(). For example, if you have a bunch of files that end up with [Series] set like:
The Expanse [S02.1080p.INFERNO]

That's easy enough to select all of them and just re-type "The Expanse". But if you have a whole pile of them all in the same format (but which each have their own individual "Series titles" and individually unique useless info in the brackets), like this:
The Expanse [S02.1080p.INFERNO]
The Expanse [S03.JERKS-2160p]
Westworld [69554832]
Letterkenny [S07-LOOKATME-1080SDR.HULU]
and so on and so forth...

Use this:
Code: [Select]
=ListItem([Series],0,/ [)
This will make a "list" with the " [" as the delimiter, and then keep only the first item in the list (index 0). The useless crap in the brackets just gets thrown away. So long as there is something "unique" in the current field value before or after or around the "pieces" you want to keep or throw away, there's usually a way to =RemoveLeft() or =ListItemI() or whatever around it.
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

timwtheov

  • Galactic Citizen
  • ****
  • Posts: 354
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #3 on: March 14, 2021, 01:43:31 pm »

Changing Parentheses to Brackets

Here's one I use a lot on non-classical tracks to change parentheses to brackets, e.g., "Thieves Like Us (12")" becomes "Thieves Like Us [12"]." It was written for me by a former user of the forum many of you long-timers might know.

And by the way, in case anyone's curious, I change parentheses to brackets to maintain AllMusic's house style, as that's typically how they do it. Here's the expression from my expression document (yes, Glynor's suggestion there is a great one):

Code: [Select]
=if(regex([Name], /#^(.*)\(([^\)]+)\)(.*)$#/), [R1][[R2]][R3], [Name])
Note that this one is used in [Name] but also includes [Name] as part of it, to update the same field you're putting it in, as per the end of Glynor's original post.
Logged

timwtheov

  • Galactic Citizen
  • ****
  • Posts: 354
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #4 on: March 14, 2021, 01:48:52 pm »

Grabbing Digits from the End of a Filename's Path

Here's another one I use a lot to quickly get [Disc #] from a filename's path on a bunch of files that have a consisten pattern of the disc number being at the end of said filename path, which happens a lot with auto-metadata retrieval. It can be easily modified to pull from [Album] too.

It's really useful for box sets:

Code: [Select]
=Right([Filename (path)], 3)
or if you only have single digits

Code: [Select]
=Right([Filename (path)], 2)
Logged

glynor

  • MC Beta Team
  • Citizen of the Universe
  • *****
  • Posts: 19608
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #5 on: March 14, 2021, 03:49:08 pm »

Here's another one I use a lot to quickly get [Disc #] from a filename's path on a bunch of files that have a consisten pattern of the disc number being at the end of said filename path, which happens a lot with auto-metadata retrieval. It can be easily modified to pull from [Album] too.

Ooh, good one! I have something similar for [Album].

Parsing [Album] to Extract and Remove (Disc XX) Text

If you have an album where they include the Disc number in the text of the [Album] tag, and you want to fill [Disc #] and then remove the extraneous info from [Album] and make them all have the same [Album] value:

Step 1 - Apply to [Disc #]:
Code: [Select]
=Number(ListItem(FixCase([Album],4),1,disc))
Step 2 - Apply to [Album]:
Code: [Select]
=FixCase(Clean(RemoveCharacters(ListItem(FixCase([Album],4),0,disc),/[/](/){}-)))
Cool thing about that is that it properly handles a bunch of different ways to note a different disc in the Album title, including:
The Wall (Disc 1)
The Wall [Disc 2]
The White Album (Disc #1)
The White Album disc 2
The Complete BBC Sessions [disc 3 - fancy version]
Tri Repetae++ - Disc 1
Tri Repetae++ - disc 2


And it automatically fixes any spacing or letter casing issues. Usually that works very well.

However, you do have to be a little careful with Step 2 (the [Album] one) for albums which use "nonstandard casing" or which contain commas because it does FixCase() to Title Case and uses Clean(). So, if you need to be more careful, just undo and then here are two simpler versions which do much the same thing but without those functions:
Code: [Select]
=FixSpacing(RemoveCharacters(ListItem([Album],0,Disc),/[/](/){}-))
=FixSpacing(RemoveCharacters(ListItem([Album],0,disc),-/[/](/){}-))

You can just run them one after another if they happen to contain a mix of upper and lowercase versions of the word "disc", because ListItem() won't "trigger" if there is no delimiter, and passes the full string through.

And, for all of these you can modify the "disc" delimiter to match whatever else your files in question might use to separate the name from the disc number (if they misspell "disc" as "disk" for example).
Logged
"Some cultures are defined by their relationship to cheese."

Visit me on the Interweb Thingie: http://glynor.com/

wer

  • Citizen of the Universe
  • *****
  • Posts: 2640
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #6 on: March 14, 2021, 04:42:01 pm »

If you like that, you'll really like this one I just proposed.

https://yabb.jriver.com/interact/index.php/topic,124543.msg894801.html#msg894801
Logged

timwtheov

  • Galactic Citizen
  • ****
  • Posts: 354
Re: Tips & Tricks: Expressions for Field Value Manipulation
« Reply #7 on: March 14, 2021, 08:09:15 pm »

That looks great, Wer! Hope it gets implemented!
Logged
Pages: [1]   Go Up