Automatic Case corrections

Download and get help for different MediaMonkey Addons.

Moderators: Peke, Gurus

jiri
Posts: 5392
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Automatic Case corrections

Post by jiri » Thu Nov 13, 2003 1:55 pm

Because there have been a number of users asking for it, I made a script that handles automatic case corrections to title, artist and albums fields. It handles all the most common situations well, if you find some problem or have a suggestion, please post a note here. As soon as the script is tested, it will be included on MM's site.

The code for FixCase.vbs file is here:

Code: Select all

' This script fixes case (Uppercase or Lowercase problems) of several fields

Dim CaseExceptions   ' Exceptions in case conversions
Set CaseExceptions = CreateObject("Scripting.Dictionary")

' The first strings are expected to be fully in lowercase
CaseExceptions.Add "to", "to"    
CaseExceptions.Add "in", "in"
CaseExceptions.Add "and", "and"
CaseExceptions.Add "on", "on"
CaseExceptions.Add "of", "of"
CaseExceptions.Add "ac", "AC"    ' Handle AC/DC name  ;-)
CaseExceptions.Add "dc", "DC"
CaseExceptions.Add "a", "a"
CaseExceptions.Add "the", "the"
CaseExceptions.Add "i", "I"       ' handle Greek numbers
CaseExceptions.Add "ii", "II"
CaseExceptions.Add "iii", "III"
CaseExceptions.Add "iv", "IV"
CaseExceptions.Add "vi", "VI"
CaseExceptions.Add "vii", "VII"
CaseExceptions.Add "viii", "VIII"
CaseExceptions.Add "ix", "IX"
CaseExceptions.Add "xi", "XI"
CaseExceptions.Add "xii", "XII"
CaseExceptions.Add "xiii", "XIII"
CaseExceptions.Add "xiv", "XIV"
CaseExceptions.Add "xv", "XV"     ' up to 15 should be enough...
CaseExceptions.Add "n", "n"       ' handle "Rock n Roll"


Function DoCaseFix( src, UpperCaseOnlyFirst)
  Dim result, pos, srclen, startpos, ch, word, first, rest, i, forcelower

  src = LCase( src)
  pos = 1
  srclen = Len( src)
  first = True
  forcelower = False

  Do While pos<=srclen
    startpos = pos
    Do While pos<=srclen
      ch = Asc( Mid( Src, pos, 1))
      If (ch<97) Or ((ch>122) and (ch<129)) Then
        Exit Do
      End If
      pos = pos + 1
    Loop

    If pos>startpos Then
      ' We found a word, check if it isn't an exception and process it accordinly
      word = Mid( src, startpos, pos-startpos)
      If CaseExceptions.Exists(word) Then
        word = CaseExceptions( word)
        If first Then
          result = result + UCase( Left( word, 1)) + Mid( word, 2)
        Else
          result = result + word
        End If
      Else
        If UpperCaseOnlyFirst And Not first Then
          ' All characters go to lower-case, except for the first one
          result = result + word
        Else
          ' Put the first letter to upper-case
          If forcelower then
            result = result + word
          Else
            result = result + UCase( Left( word, 1)) + Mid( word, 2)
          End If
        End If
      End If
    End If

    first = False
    forcelower = False

    startpos = pos
    Do While pos<=srclen
      ch = Asc( Mid( Src, pos, 1))
      If Not((ch<97) Or ((ch>122) and (ch<129))) Then
        Exit Do
      End If
      pos = pos + 1
    Loop

    ' simply copy all other characters
    rest = Mid( src, startpos, pos-startpos)
    i = Len( rest)
    If i>0 Then
      ch = Asc( Right( rest, 1))
      If ch=46 Then  ' '.' causes the next letter to be uppercased
        first = True
      End If
      If (ch=39) or (ch=96) Then
        forcelower=True
      End If
    End If
    result = result + rest
  Loop

  DoCaseFix = result
End Function

Sub FixCaseAll
  ' Define variables
  Dim list, itm, i, tmp

  ' Get list of selected tracks from MediaMonkey
  Set list = SDB.SelectedSongList 
  If list.count=0 Then
    Set list = SDB.AllVisibleSongList
  End If

  ' Process all selected tracks
  For i=0 To list.count-1
    Set itm = list.Item(i)

    ' Fix case for some fields
    itm.ArtistName = DoCaseFix( itm.ArtistName, False)   ' do artist and album at first as they can cause re-read of data
    itm.AlbumName = DoCaseFix( itm.AlbumName, False)
    itm.Title = DoCaseFix( itm.Title, False)

    ' Update the changes in DB
    itm.UpdateDB
  Next
End Sub

Sub FixCaseFirst
  ' Define variables
  Dim list, itm, i, tmp

  ' Get list of selected tracks from MediaMonkey
  Set list = SDB.SelectedSongList 
  If list.count=0 Then
    Set list = SDB.AllVisibleSongList
  End If

  ' Process all selected tracks
  For i=0 To list.count-1
    Set itm = list.Item(i)

    ' Fix case for some fields
    itm.ArtistName = DoCaseFix( itm.ArtistName, True)   ' do artist and album at first as they can cause re-read of data
    itm.AlbumName = DoCaseFix( itm.AlbumName, True)
    itm.Title = DoCaseFix( itm.Title, True)

    ' Update the changes in DB
    itm.UpdateDB
  Next
End Sub
And this must be added to Scripts.ini:

Code: Select all

[FixCaseAll] 
FileName=FixCase.vbs 
ProcName=FixCaseAll
Order=16
DisplayName=Start all words in &Upper-case
Description=Start all words in upper-case
Language=VBScript 
ScriptType=0

[FixCaseFirst]
FileName=FixCase.vbs 
ProcName=FixCaseFirst
Order=17
DisplayName=Start the first word in U&pper-case
Description=Start the first word in upper-case
Language=VBScript 
ScriptType=0
Jiri

Lowlander
Posts: 46171
Joined: Sat Sep 06, 2003 5:53 pm
Location: MediaMonkey 5

Questions

Post by Lowlander » Thu Nov 13, 2003 5:16 pm

This is great, but I have two questions:

Does this update the tags in the files too (or do you need to run synchronize feature afterwards)

How similar is VBscript to VB 6 and VB.net?
Lowlander (MediaMonkey user since 2003)

jiri
Posts: 5392
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri » Thu Nov 13, 2003 5:32 pm

It updates tags as well if the option is turned in the Options dialog (which is by default).

VBScript syntax is very similar to VB6, there are some differences, but particularly in the basics it's the same. VB.Net has some new features like exception handling, etc.

Jiri

dastriebel
Posts: 6
Joined: Wed Nov 12, 2003 1:55 pm
Location: Switzerland

Post by dastriebel » Sat Nov 15, 2003 10:06 am

This is great, thanks...
As programmer (developing with Microsoft Access since Version 1.1, working with VBS for 2 or 3 years) i will have a close look at the SDB-objectmodell.

Thanks

scotter
Posts: 3
Joined: Mon Mar 01, 2004 2:56 am
Location: Spokane, WA

Is there wa way to upperc actual file names, not just MM Db?

Post by scotter » Mon Mar 01, 2004 3:02 am

Jiri,
I used your script, it works well. :lol: I was wondering though if there is a way to uppercase the first letter of each word of the file name? ben folds five-brick.mp3 gets the MM DB instance changed to Ben Folds...and the Tag gets Ben Folds...but the file name stays the same. I am sort of old school and go through Win Explorer queing things up for WM9 as much as I go through MM or Winamp or whatever. It would be nice to have DB, Tag, and File name all match.

Do you know a way to do that? I don't know much scripting.... :cry:
I think the RIAA is after me.

jiri
Posts: 5392
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri » Mon Mar 01, 2004 5:59 am

There were two things added in MM 2.2 scripting (starting from alpha 3 release) that should help in this:

- When 'Path' property of Song object is modified, it isn't modified in DB only, but also the actual filename is modified.
- The same object has a new method 'RenameByMask' that can rename given track according to the mask - non-user friednly mask version has to be used in order to prevent localization problems, e.g.
Item.RenameByMask( '%A - %S')

Jiri

scotter
Posts: 3
Joined: Mon Mar 01, 2004 2:56 am
Location: Spokane, WA

renaming case

Post by scotter » Mon Mar 01, 2004 11:21 pm

jiri wrote:There were two things added in MM 2.2 scripting (starting from alpha 3 release) that should help in this:

- When 'Path' property of Song object is modified, it isn't modified in DB only, but also the actual filename is modified.
- The same object has a new method 'RenameByMask' that can rename given track according to the mask - non-user friednly mask version has to be used in order to prevent localization problems, e.g.
Item.RenameByMask( '%A - %S')

Jiri
I believe I have the newest version of Media Monkey and I use your script through the Tools > Scripts menu. It seems to work in the DB and the MP3 tag (though it is seeming to hang on doing 700 files - 1 gig - at once) but the file name stays the same. Is there something I should edit in the script?

I am going through my singles now, cleaning up the tags, and MM DB info. Assuming I get that all correct I guess I could then use Auto-Rename File command. That would take the Upper Case artist and track info and rename the file. But it would be nice to be able to use the Auto-Tag from Filename function too (but currently alot of the filenames have all lowercase, which is my original problem)

I guess what I am saying is that I don't know how to impliment the function you describe in your last post.

Thanks in advance for the help.
-s
I think the RIAA is after me.

jiri
Posts: 5392
Joined: Tue Aug 14, 2001 7:00 pm
Location: Czech Republic
Contact:

Post by jiri » Tue Mar 02, 2004 2:01 am

No, you definitely don't have this version yet. As I wrote, it's implemented since MM 2.2 alpha 3 which will be hopefully released tonight. Note that it's just an alpha version - mainly targeted for testing.

Jiri

Post Reply