INTERACT FORUM

Please login or register.

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

Author Topic: [FIXED] [Bug report] ListItem() and ListCount() woes  (Read 3659 times)

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
[FIXED] [Bug report] ListItem() and ListCount() woes
« on: March 18, 2012, 06:04:20 am »

Hi all,

I was wondering if there was a better way of building my scripts in the expression language.

I am aware that when you are renaming files/folders, there is an "expression editor" which really does nothing much except put up a full window for you to edit your script and allows you to select functions and fields in your library. It doesn't allow you to tab your expressions or show nesting.
i.e.
if(condition,
doSomething,
else
)
If you do that, I believe the editor would add spaces to all your return carriages. (I am not tabbing them)
I find myself having a better time using an external editor which tells me important scripting information such as where does a bracket start/end like a typical IDE for C/Java.

So the question is,
Am I not aware of a script testing environment in MC17 which would make life a lot easier?


listItem() woes...
The reason why I'm asking is because I'm having a bit of a challenge trying to get the following to work.
I've got a custom field [Franchise], datatype is List (semicolon delimited)

I have a file which has the following:
[Franchise]=>Batman;Batman Chris Nolan
(It shows up nicely in my tag window lower left of screen as a list)

When I use the expression
listCount([Franchise]) ===> 1
?
I go further to
listItem([Franchise],0) ===> Batman
Yup that's fine...
listItem([Franchise],1) ===> NOTHING

Am I doing something wrong here?
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.

In MC, probably the most efficient way to create / test expressions is by using multiple expression columns, each later column building on the previous column's success.  This gives you almost instant results to sub-expressions, where you can advance upon success.

Inside the expression editor, to help visually, you can use whitespace freely after a comma.  It is limited, but better than rotten eggs.

If(<test>,
   <expr TRUE>,
   <expr FALST>)

I've probably been one of the most vocal nuisances to the JRiver team about these limitations.  In their defense, the MC language was not designed to accommodate this, language parsing is non-trivial, and supporting a code-editor is not their bread-and-butter.  So we work around these issues.

Just in case you don't know about this:

  http://wiki.jriver.com/index.php/Media_Center_expression_language

Re: your [Franchise] field, from release notes:

Quote
17.0.76 (1/25/2012)

2. Changed: List fields can be configured to use a standard edit or list edit style. Existing user created list-type fields will need to be updated in Options > Library & Folders > Manage library fields > Edit Type > List if you want a list-type editor for them.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76

Hi MrC,

Thanks for the reply and suggestions.

The
if(someTest(),
   doIfTrue,
   doIfFalse)
is really helpful and I will use that indentation method for my own script/function development.
I currently write everything in notepad before pasting it into the rename textbox.

Where I meet up with a challenge is when I use the following
if(true,1,0)
if(false,1,0)
if(true,1,0)

The result will be 1 0 1 instead of 101. (i.e. The return carriage gets substituted with a space instead of throwing it out during the script parsing.)

I am very aware that developing a full blown editor is asking for too much since MC is first and foremost an amazing Media player. IDEs are usually a whole project/product in their own.  ;D


http://wiki.jriver.com/index.php/Media_Center_expression_language
I actually read the media expression language really often myself. It is like reading at least the basic key API (e.g. String commands, System.out. commands etc.) of any scripting/programming language.

To be honest...my script used to work before, that's why I am pulling hair out wondering what went wrong this time.

Actually, I went back to try my script on a file which hasn't had any updates in metadata and I have successfully used listCount() and listItem() successfully so it appears that there has been a change with MC17 somewhere within the past 6 months.

17.0.76 (1/25/2012)

2. Changed: List fields can be configured to use a standard edit or list edit style. Existing user created list-type fields will need to be updated in Options > Library & Folders > Manage library fields > Edit Type > List if you want a list-type editor for them.


Before I created the thread, I have already tried fiddling with Options>Library/Folders>Manage Library Fields. It doesn't make a different. What it appears to me is simply the difference between having checkboxes to select/add new data or just typing itemOne;itemTwo;itemThree.



If this is actually a new bug in MC17, is this the right place for me to raise it?
Logged

Vincent Kars

  • Citizen of the Universe
  • *****
  • Posts: 1154

Quote
In MC, probably the most efficient way to create / test expressions is by using multiple expression columns, each later column building on the previous column's success.

This is what I do most of the time.
Develop the expression first in a Expression column.
However your post suggest one can refer to this expression column.

If I remember correctly using =[My Expression] in any other column didn’t work.

Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.

Where I meet up with a challenge is when I use the following
if(true,1,0)
if(false,1,0)
if(true,1,0)

The result will be 1 0 1 instead of 101. (i.e. The return carriage gets substituted with a space instead of throwing it out during the script parsing.)

Yes, that's correct.  Only whitespace after a comma is irrelevant.  Each case of code will require slightly different formatting, and it won't always be perfect. In your case above, I'd use:

if(true,1,0)if(false,1,0)if(true,1,0)

or

if(true,
  1,
  0)if(false,
  1,
  0)if(true,
  1,
  0)

so at least you can see the output values in order they'll be presented, appearing as labels, with the tests less prominent.  Often IfElse() can create better flow for nested If() statements.

Re: the [Franchise] field.  Did you change the Edit Type, exit MC, and then test it?  Then change it back and test it?


However your post suggest one can refer to this expression column.

If I remember correctly using =[My Expression] in any other column didn’t work.

Never meant to imply that, sorry for any confusion.  I meant get one simple expression working, and then copy the working code into the next expression column's editor (at the proper location).  Thanks for pointing out possible ambiguity.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76

This is what I do most of the time.
Develop the expression first in a Expression column.
However your post suggest one can refer to this expression column.

If I remember correctly using =[My Expression] in any other column didn’t work.



Hi Vincent,

Your welltempered computer site was what inspired me to roll up my sleeves to fiddle with the scripts. You've got really great examples there.

Off my head, in addition to MrC who is helping me with this, I can remember VincentKhar, Gappie and MusicHawk providing AMAZING examples to work on I wish I sat down to copy all the knowledge shared from older posts.
Logged

javidan

  • Junior Woodchuck
  • **
  • Posts: 76

Yes, that's correct.  Only whitespace after a comma is irrelevant.  Each case of code will require slightly different formatting, and it won't always be perfect. In your case above, I'd use:

if(true,1,0)if(false,1,0)if(true,1,0)

or

if(true,
  1,
  0)if(false,
  1,
  0)if(true,
  1,
  0)

so at least you can see the output values in order they'll be presented, appearing as labels, with the tests less prominent.  Often IfElse() can create better flow for nested If() statements.

Re: the [Franchise] field.  Did you change the Edit Type, exit MC, and then test it?  Then change it back and test it?

Hi MrC,

Thanks again!
I will definitely be using the indentation method you described for more complicated nesting in the future. It is so useful to know that the parser allows you to do a return carriage after a comma. I know it's definitely anything but perfect but I can't tell you how much I appreciate it. I am not the type of person who can view code written in a long line and make sense of it. (i.e. I am a terrible coder.)

I have fiddled with the edit types for the [Franchise] field. I have even made changes to the field, tried it on different files.

I will install MC16 again tomorrow. If MC16 somehow works...then I will try to see if it's a bug in MC17 or that a new change made it such that list fields have to be interpreted differently.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.

Glad it works out for you.  I've made a secret request to the devs for some more support here - let's see if it rains gold.

If you want to send me a backup copy of the library, I can see what I can determine.  PM me a location of the backup library file if so.  Otherwise, no need to act.
Logged
The opinions I express represent my own folly.

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.

I've made a secret request to the devs for some more support here - let's see if it rains gold.

I see gold in our futures.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #9 on: March 23, 2012, 02:23:59 am »

Hello,

It started off as a post wondering if I did something wrong with my script but I've just reinstalled MC16 and tried the same script on the same file and ListCount() and ListItem() does appear to work the way it intends to so I've renamed the subject accordingly.

Basically I've got a library field called "Franchise" which is a semi-colon delimited list.

Even with two members in the list ListCount() seems to give me 1. ListItem(1) returns nothing.
Playing around with changing the way the field is edited (either Standard or CheckBox) doesn't change anything.
As reported earlier:

I have a file which has the following:
[Franchise]=>Batman;Batman Chris Nolan
(It shows up nicely in my tag window lower left of screen as a list)

When I use the expression
listCount([Franchise]) ===> 1

I go further to
listItem([Franchise],0) ===> Batman
Yup that's fine...
listItem([Franchise],1) ===> NOTHING
Logged

javidan

  • Junior Woodchuck
  • **
  • Posts: 76

Glad it works out for you.  I've made a secret request to the devs for some more support here - let's see if it rains gold.

If you want to send me a backup copy of the library, I can see what I can determine.  PM me a location of the backup library file if so.  Otherwise, no need to act.

Thanks MrC. :)

I didn't want to trouble you if it was something I could do myself (such as reinstalling MC16 to see if it is any different)

I will bug you when I'm trying out weirder scripts later.  ;D
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #11 on: March 23, 2012, 01:43:35 pm »

Try adding a space after any semicolon in the value of your Franchise field.  Test on a few files.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #12 on: March 24, 2012, 12:27:02 am »

I already did.
I have also gone to fiddle with a TV Series instead of a single file.

I went further into testing based on the advice from you and Vincent using Expression Columns as a semi debug tool and realised that this bug is specific to the rename dialog box.

You will see quite clearly that in an expression column, I can create a semicolon delimited field and use ListCount() and ListItem() correctly but it fails when I use the same function in the rename dialog box.
More specifically ListCount() returns 1 when the same function in an expression column returns 2.

Here are some screenshots which would probably explain it a lot better than if I were to try to type it out.

"Works fine in Column" shows that
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #13 on: March 24, 2012, 02:04:08 am »

Recent changes to [Artist] from a string to a list style required some places to use only the first entry in the list.  This seems like one of the places.

Perhaps one of the dev's will comment.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #14 on: March 24, 2012, 10:31:33 am »

Yeah. :(

Well it's definitely something which works in MC16 (probably early MC17) and currently broken.

And this field is a custom user created field by me.  :-\
Logged

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42387
  • Shoes gone again!
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #15 on: March 25, 2012, 08:18:39 am »

I'll test in the debugger Monday and follow-up then.

Thanks.
Logged
Matt Ashland, JRiver Media Center

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42387
  • Shoes gone again!
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #16 on: March 26, 2012, 10:03:45 am »

When building filenames, only the first item is used from a list field.

This was this change:

17.0.83 (2/6/2012)
Changed: When building a filename from file information, only the primary / first artist or genre will be used from a list.


As a work-around for you, we'll make it so that using [Franchise, 0] (which is shorthand for Field(Franchise, 0) will get the full list.  By default, the second parameter is '1' which means format for display, and '0' means get the raw data.

Look for this in 17.0.114 and newer.
Logged
Matt Ashland, JRiver Media Center

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #17 on: March 26, 2012, 01:25:30 pm »

17.0.83 (2/6/2012)
Changed: When building a filename from file information, only the primary / first artist or genre will be used from a list.

As a work-around for you, we'll make it so that using [Franchise, 0] (which is shorthand for Field(Franchise, 0) will get the full list.  By default, the second parameter is '1' which means format for display, and '0' means get the raw data.

Ah, sorry I missed that change.

So the new change will be to generally allow [fieldname,0] to work?
Logged
The opinions I express represent my own folly.

Matt

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 42387
  • Shoes gone again!
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #18 on: March 26, 2012, 05:07:00 pm »

So the new change will be to generally allow [fieldname,0] to work?

That's always been a syntax for getting raw data.

In this case, conversion of a list to only the first item will be disabled when requesting raw data.

Does that make sense?
Logged
Matt Ashland, JRiver Media Center

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #19 on: March 26, 2012, 05:15:29 pm »

Yup.  By "to work", I meant "to work... in the Rename dialog".

Thanks.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #20 on: March 27, 2012, 07:43:44 am »

Quote
17.0.83 (2/6/2012)
Changed: When building a filename from file information, only the primary / first artist or genre will be used from a list.

For my own understanding...Can I rephrase the same comment to something like:
"Rename, Copy & Move Files" Dialog box behaviour changed
Fields which are lists will be truncated to just the first member of the list.



Thus with this new change, ListCount([ListedFieldName]) always returns 1 instead of the actual number in the past.
Which begs me to wonder why this change was done. Now the same function/method on the same field would return different results. ?

So now instead of using [ListedFieldName], I will use [ListedFieldName, 0] just for the file rename dialog box?

My renaming script logic has this sort of flow

Code: [Select]
if (ListCount[ListFieldName]=0)//Using J River wiki/forum example it is usually if(isEqual([ListFieldName],Unknown,7)
  doThis
if (listCount[ListFieldName]=1)
  doThisinstead
if (listCount[ListFieldName]=2)
  doYetAnother

I use the length of the list in the field to help myself with types of logic.

For example I may also do

Code: [Select]
if (ListCount[Artist]==2)
  putInsideDuetsFolder

So with the new release I should change my code to
Code: [Select]
//If you have a sub-franchise, add the sub-franchise to the folder-build string
if(listCount([Franchise, 0]>1),listItem([Franchise, 0],1))
for it to work right?
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #21 on: March 27, 2012, 11:43:06 am »

There were several reasons for the initial change, all a result of the complex changing of fields Artist, Genre, etc. from simple string fields to list fields.  This created conflicts for some people and for MC itself under certain circumstances.

For example, [Genre] is typically thought of as being singular for music.  And some people use file location rules to organize their music, based on genre.  They expect the music to stay in the same place after the change from string to list type.  The auto-calculated field Album Artist (auto) requires likewise. To accommodate this, in some cases, expansion of the fields needs to provide only the first item in the list, as the primary item.

The pass at making the string to list changes hit most of the affected code areas, roughly.  Now real-usage is showing where the rough spots need to be smoothed.

Lists are funny things in MC - they are treated by the UI essentially as just strings, with some separator characters.  So functions like ListCount() can just run through the string and count, split, return an item.  And you can manipulate/create them with other functions.  While this is a special, simple implementation, it creates some curious side affects.  What would it mean, otherwise, to say a given folder path component should be based on some "list"?  Does the list get "flattened"?  How are separators included/chosen during flattening?, What is the order of list items?  What happens when the interpolated list's length is too long?  These are just some of the issues that have to be managed, and they become complex when they're spread throughout a large code base.

So, if you want the full list (or raw data from a field), use the [fieldname,0] notation.  Otherwise, the default [fieldname,1] (formatted data) expansion is used.

Now, if you post your full expression, we might be able to show you how to simplify it.  Given its description, I have some ideas.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #22 on: March 27, 2012, 07:40:13 pm »

I use the following code in the rule for Directory.

Code: [Select]
if(isEqual([Franchise],Unknown,7),,listItem([Franchise],0) FRANCHISE\if((listCount([Franchise])>1),listItem([Franchise],1)\,))if(isEqual([Media Sub Type],Movie,7),[Name],) if(isEqual([Media Sub Type],TV Show,7),[Series]if(isEqual([Video Standard],Unknown,7),,/ [[Video Standard]]),)
I use Franchise for initial folder creation.
All my Batman movies/series etc will be found in my Batman FRANCHISE folder
The Harry Potter movies will be found in the Harry Potter FRANCHISE folder

Some franchises such as the Buffy series has spawned off sub franchises such as the Buffy TV Series and Angel TV Series which will be the 2nd member of the list.
They will be in the Buffy FRANCHISE\Buffy TV Series and Buffy FRANCHISE\Angel TV Series respectively
(Assuming I have "Buffy; Buffy TV Series" and "Buffy; Angel TV Series" in the field Franchise)

Thanks for your help.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [BUG REPORT] ListItem() and ListCount()
« Reply #23 on: March 30, 2012, 04:31:54 pm »

I received your PM, so here's the follow-up to...

I use the following code in the rule for Directory.

 <code snipped>

I use Franchise for initial folder creation.
All my Batman movies/series etc will be found in my Batman FRANCHISE folder
The Harry Potter movies will be found in the Harry Potter FRANCHISE folder

Some franchises such as the Buffy series has spawned off sub franchises such as the Buffy TV Series and Angel TV Series which will be the 2nd member of the list.
They will be in the Buffy FRANCHISE\Buffy TV Series and Buffy FRANCHISE\Angel TV Series respectively
(Assuming I have "Buffy; Buffy TV Series" and "Buffy; Angel TV Series" in the field Franchise)

Thanks for your help.

And via PM we said:

Quote from: MrC
How many items might be in [Franchise]?  More than 2?

Quote from: javidan
It could be more than two but I am aware of my script only reading the first two.

The sequence is obviously important to me due to my hard-code.

Batman; Batman Reboot, and
Batman Reboot; Batman

Are going to wind up in different places so I take a lot of care to ensure the most important franchise comes first.

So, given that [Franchise] may contain more than 2 items, we'll generalize too allow any depth.  Here's the code:

ifelse(
   !isempty([Franchise]),
      replace(regex([Franchise,0], /#^([^;]+)(.*)$#/, -1)/
         [R1] FRANCHISE[R2], ;/ , \)/
)/
ifelse(
     isEqual([Media Sub Type], Movie),      \[Name],
     isEqual([Media Sub Type], TV Show),  \[Series])/
ifelse(
    !isEqual([Video Standard], Unknown),   / [[Video Standard]])

1. If there is a non-empty Franchise, then combine the individual list items into a backslash separated path, adding the word FRANCHISE after the first component.
2. Add Name or Series, depending upon Media Sub Type.
3. And finally tack on the Video Standard in brackets if it is set (you may want to tweak this).

I think I have the pieces as you've wanted them - but double check and tweak as necessary.
Logged
The opinions I express represent my own folly.

javidan

  • Junior Woodchuck
  • **
  • Posts: 76
Re: [FIXED] [Bug report] ListItem() and ListCount() woes
« Reply #24 on: March 30, 2012, 10:36:49 pm »

Oh wow...

Code: [Select]
ifelse(
   !isempty([Franchise]),
      replace(regex([Franchise,0], /#^([^;]+)(.*)$#/, -1)/
         [R1] FRANCHISE[R2], ;/ , \)/
)/

Is a function based list enumerator?
I've seen you use regex() function twice now but I could not wrap my head around it.
I need to peruse the expression language wiki with more detail on that part.

Will test it.

My default "kid's script" now works after Matt added the [Franchise, 0] but I'm definitely interested in cleaning up my code to something more elegant.
Logged

MrC

  • Citizen of the Universe
  • *****
  • Posts: 10462
  • Your life is short. Give me your money.
Re: [FIXED] [Bug report] ListItem() and ListCount() woes
« Reply #25 on: March 30, 2012, 10:45:12 pm »

Is a function based list enumerator?

I'm not sure I understand.

The expression takes your franchises such as:

  Batman; Batman Reboot
  Harry Potter
  Buffy; Buffy TV Series
  Buffy; Angel TV Series
  Something; with; any; number; of; items

and turns them into these:

  Batman FRANCHISE\Batman Reboot\
  Harry Potter FRANCHISE\
  Buffy FRANCHISE\Buffy TV Series\
  Buffy FRANCHISE\Angel TV Series\
  Something FRANCHISE\with\any\number\of\items\
Logged
The opinions I express represent my own folly.
Pages: [1]   Go Up