Advanced Rename 2.3 [MM2+3]

Download and get help for different MediaMonkey for Windows 4 Addons.

Moderators: Peke, Gurus

trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Advanced Rename 2.3 [MM2+3]

Post by trixmoto »

This is a custom script I think others might enjoy. It allows you to rename artists "Firstname Lastname" to "Lastname, Firstname". This of course works with middlenames: "Firstname Middlename Lastname" -> "Lastname, Firstname Middlename".

It allows you to list three kinds of exception:
1) Exception: whole artists which should not be renamed
2) Prefixes: firstname which should always be firstname
3) Postfixes: lastname which should always be lastname

Here are a sample of artists and how they work (if you have your exceptions setup correctly):
Al Jolson -> Jolson, Al
Chet Baker -> Baker, Chet
John Lee Hooker -> Hooker, John Lee
Billie Holiday -> Holiday, Billie
Grover Washington Jr. -> Washington, Grover Jr. ('Jr.'=postfix)

As always an installer is available from my website.

Code: Select all

'
' MediaMonkey Script
'
' NAME: Advanced Rename 2.3
'
' AUTHOR: trixmoto (http://trixmoto.net)
' DATE : 17/08/2008
'
' Form copied from "TitleCase.vbs", March-22-2004, v1.0, written by Risser 
'
' INSTALL: Copy to Scripts directory and add the following to Scripts.ini 
'          Don't forget to remove comments (') and set the order appropriately
'          Check the variables below to ensure appropriate settings
'
' FIXES: Fixed the handling of multiple artists
'
' [AdvancedRename]
' FileName=AdvancedRename.vbs
' ProcName=AdvancedRename
' Order=14
' DisplayName=Advanced Rename
' Description="Firstname Lastname" -> "Lastname, Firstname"
' Language=VBScript
' ScriptType=0 
'

Option Explicit 

Dim delim : delim = ", "

Dim mode
mode = 0 'Firstname Lastname -> Lastname, Firstname
'mode = 1 'Lastname, Firstname -> Firstname Lastname

Dim prefixes, postfixes, exceptions	'BAR (|) delimited lists
prefixes = ""				'single words only
postfixes = "Jr.|Junior"		'single words only
exceptions = "Mr T"			'whole artist names

Public holdArtist, holdAlbumArtist, holdPrefixes, holdPostfixes, holdExceptions 
Set holdArtist = CreateObject("Scripting.Dictionary") 
Set holdAlbumArtist = CreateObject("Scripting.Dictionary") 
Set holdPrefixes = CreateObject("Scripting.Dictionary")
Set holdPostfixes = CreateObject("Scripting.Dictionary")
Set holdExceptions = CreateObject("Scripting.Dictionary")

Const mmAnchorRight = 4 
Const mmAnchorBottom = 8 
Const mmAlignTop = 1 
Const mmAlignBottom = 2 
Const mmAlignClient = 5 
Const mmListDropdown = 2 
Const mmFormScreenCenter = 4 
Public styleOn 

Function SetPrefixes(str)
  Dim list,i
  list = split(str,"|")  
  For i = 0 to UBound(list)
    If not holdPrefixes.Exists(list(i)) Then
      holdPrefixes.add list(i),list(i)
    End If
  Next
  SetPrefixes = holdPrefixes.Count
End Function
    
Function SetPostfixes(str)
  Dim list,i
  list = split(str,"|") 
  For i = 0 to UBound(list)
    If not holdPostfixes.Exists(list(i)) Then
      holdPostfixes.add list(i),list(i)
    End If
  Next
  SetPostfixes = holdPostfixes.Count
End Function

Function SetExceptions(str)
  Dim list,i
  list = split(str,"|")
  For i = 0 to UBound(list)
    If not holdExceptions.Exists(list(i)) Then
      holdExceptions.add list(i),list(i)
    End If
  Next
  SetExceptions = holdExceptions.Count
End Function

Function Style() 
  styleOn = Not styleOn 
  If styleOn Then 
    Style = "" 
  Else 
    Style = " class=""Dark""" 
  End If 
End Function 

Function rdQS(UnquotedString) 
  rdQS = "'" & Replace(UnquotedString, "'", "''") & "'" 
End Function 

Function rename(str) 'Firstname Lastname -> Lastname, Firstname
  Dim pre, post, word, pos
  rename = str
  pre = ""
  post = ""
  word = ""
  pos = InStr(str," ")
  If pos > 0 Then		'if there are multiple words
    If not holdExceptions.Exists(str) Then
      word = Mid(str,1,pos-1)
      If holdPrefixes.Exists(word) Then
        str = Mid(str,pos+1)
        pre = word&" "
      End If
      pos = InStrRev(str," ")
      If pos > 0 Then		'if there is a last word
        word = Mid(str,pos+1)
        If holdPostfixes.Exists(word) Then
          str = Mid(str,1,pos-1)
          post = " "&word
        End If
      End If
      pos = InStrRev(str," ")
      If pos > 0 Then		'if there are still multiple words
        word = Mid(str,pos+1)
        str = word&delim&Mid(str,1,pos-1)
      End If
      rename = pre&str&post
    Else
'      MsgBox "Exception: "&str
    End If
  Else
'    MsgBox "Only one word: "&str
  End If
End Function 

Function rename2(str) 'Lastname, Firstname -> Firstname Lastname
  Dim pre, post, word, pos
  rename2 = str
  pre = ""
  post = ""
  word = ""
  pos = InStr(str,delim)
  If pos > 0 Then	'if there is a comma
    If not holdExceptions.Exists(str) Then
      post = Mid(str,1,pos-1)
      pre = Mid(str,pos+Len(delim))
      rename2 = pre&" "&post
    Else
'      MsgBox "Exception: "&str
    End If
  Else
'    MsgBox "No comma: "&str
  End If
End Function 

Sub CloseDown 
   Set holdAlbumArtist = Nothing 
   Set holdArtist = Nothing 
   Set holdExceptions = Nothing
   Set holdPrefixes = Nothing
   Set holdPostfixes = Nothing
   SDB.Objects("AdvancedRenameForm") = Nothing 
   SDB.Objects("holdArtist") = Nothing 
   SDB.Objects("holdAlbumArtist") = Nothing 
End Sub 

Sub OnCancel(Btn) 
   CloseDown 
End Sub 

Sub OnOK(Btn) 
   Set holdAlbumArtist = SDB.Objects("holdAlbumArtist") 
   Set holdArtist = SDB.Objects("holdArtist") 

   Dim itm, str, sql 
   Dim items, artistNames 
   Set items = CreateObject("Scripting.Dictionary") 
   Set artistNames = CreateObject("Scripting.Dictionary") 

   For Each itm In holdArtist 
      str = holdArtist.item(itm) 
      If Not items.exists(itm) Then 
         items.add itm, itm 
      End If 
      itm.artistName = str 
      If Not artistNames.exists(str) Then  
         artistNames.add str, str 
      End If 
   Next 'itm 
    
   For Each itm In holdAlbumArtist 
      str = holdAlbumArtist.item(itm) 
      If Not items.exists(itm) Then 
         items.add itm, itm 
      End If 
      itm.albumArtistName = str 
      If Not artistNames.exists(str) Then 
         artistNames.add str, str 
      End If 
   Next 'itm 
    
   Dim list : Set list = SDB.NewSongList
   For Each itm In items 
      If itm.ID>-1 Then 
         itm.UpdateArtist
         itm.UpdateAlbum
         list.Add(itm)
      End If 
   Next 'itm    
   list.UpdateAll   
    
   Set items = nothing 
   CloseDown 
End Sub 

Function MapXML(original) 
   Dim hold 
   hold = Replace(original, "&", "&") 
   hold = Replace(hold, "  ", "  ") 
   hold = Replace(hold, "<", "<") 
   hold = Replace(hold, ">", ">") 
   Dim i 
   i=1 
   While i<=Len(hold) 
      If (AscW(Mid(hold, i, 1))>127) Then 
         hold = Mid(hold, 1, i-1)+"&#"+CStr(AscW(Mid(hold, i, 1)))+";"+Mid(hold, i+1) 
      End If 
      i=i+1 
   WEnd 
   MapXML = hold 
End Function 

Function outField (fixed, normal) 
   If fixed = normal Then 
      outField = "<td>" & MapXML(normal) & "</td>" & vbcrlf 
   Else 
      outField = "<td class=""highlight"">" & MapXML(fixed) & "</td>" & vbcrlf 
   End If 
End Function 


Sub AdvancedRename 
   Dim UI, Form, Foot, Btn, Btn2, WB, doc 
    
   Dim trackList 
   Dim writeChanges 

   Set trackList = SDB.SelectedSongList 
   If trackList.count=0 Then 
      Set trackList = SDB.AllVisibleSongList 
   End If 

   If trackList.count=0 Then 
      res = SDB.MessageBox("Select tracks to be updated", mtError, Array(mbOk)) 
      Exit Sub 
   End If 
   
   SetPrefixes(prefixes)
   SetPostfixes(postfixes)
   SetExceptions(exceptions)

   Set UI = SDB.UI 

   ' Create the window to be shown 
   Set Form = UI.NewForm 
   Form.Common.SetRect 50, 50, 500, 400 
   Form.Common.MinWidth = 200 
   Form.Common.MinHeight = 150 
   Form.FormPosition = mmFormScreenCenter 
   Form.SavePositionName = "AdvancedRenameWindow" 
   Form.Caption = SDB.Localize("Advanced Rename") 
   Form.StayOnTop = True 

   ' Create a web browser component 
   Set WB = UI.NewActiveX(Form, "Shell.Explorer") 
   WB.Common.Align = mmAlignClient      ' Fill all client rectangle 
   WB.Common.ControlName = "WB" 
   If SDB.VersionHi=2 and SDB.VersionLo<5 Then WB.Interf.Navigate "about:"
   Set doc = WB.Interf.Document

   ' Create a panel at the bottom of the window 
   Set Foot = UI.NewPanel(Form) 
   Foot.Common.Align = mmAlignBottom 
   Foot.Common.Height = 35 

   ' Create a button that closes the window 
   Set Btn = UI.NewButton(Foot) 
   Btn.Caption = SDB.Localize("&Cancel") 
   Btn.Common.SetRect (Foot.Common.Width - 180)/2+95, 9, 85, 24 
   Btn.Common.Anchors = mmAnchorRight + mmAnchorBottom 
   Btn.UseScript = Script.ScriptPath 
   Btn.OnClickFunc = "OnCancel" 

   ' Create a button that saves the report 
   Set Btn2 = UI.NewButton(Foot) 
   Btn2.Caption = SDB.Localize("&OK")  
   Btn2.Common.SetRect (Foot.Common.Width - 180)/2, 9, 85, 24 
   Btn2.Common.Anchors = mmAnchorRight + mmAnchorBottom 
   Btn2.UseScript = Script.ScriptPath 
   Btn2.OnClickFunc = "OnOK"  

   Form.Common.Visible = True                ' Only show the form, don't wait for user input 
   SDB.Objects("AdvancedRenameForm") = Form  ' Save reference to the form somewhere, otherwise it would simply disappear 


   doc.write "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">" & vbcrlf 
   doc.write "<html>" & vbcrlf 
   doc.write "  <head>" & vbcrlf 
   doc.write "    <title>" & SDB.Localize("Advanced Rename") & "</title>" & vbcrlf 
   doc.write "  </head>" & vbcrlf 

   doc.write "<STYLE TYPE=text/css>" & vbcrlf 
   doc.write "body{font-family:'Verdana',sans-serif; background-color:#FFFFFF; font-size:9pt; color:#000000;}" & vbcrlf 
   doc.write "H1{font-family:'Verdana',sans-serif; font-size:13pt; font-weight:bold; color:#AAAAAA; text-align:left}" & vbcrlf 
   doc.write "P{font-family:'Verdana',sans-serif; font-size:8pt; color:#000000;}" & vbcrlf 
   doc.write "TH{font-family:'Verdana',sans-serif; font-size:9pt; font-weight:bold; color:#000000; border-color:#000000; border-style: solid; border-left-width:0px; border-right-width:0px; border-top-width:0px; border-bottom-width:3px;}" & vbcrlf 
   doc.write "TD{font-family:'Verdana',sans-serif; font-size:8pt; color:#000000; border-color:#000000; border-style: solid; border-left-width:0px; border-right-width:0px; border-top-width:0px; border-bottom-width:1px;}" & vbcrlf 
   doc.write "TD.highlight{font-family:'Verdana',sans-serif; font-size:8pt; background-color:#FFFF77; color:#000000; border-color:#000000; border-style: solid; border-left-width:0px; border-right-width:0px; border-top-width:0px; border-bottom-width:1px;}" & vbcrlf 
   doc.write "TR.dark{background-color:#EEEEEE}" & vbcrlf 
   doc.write "TR.aleft TH{text-align:left}" & vbcrlf 
   doc.write "</STYLE>" & vbcrlf 

   doc.write "  <body>" & vbcrlf 
   doc.write "    <H1>" & SDB.Localize("Advanced Rename:") & "</H1>" & vbcrlf 
   doc.write "    <table border=""0"" cellspacing=""0"" cellpadding=""4"" width=""100%"">" & vbcrlf  
   doc.write "      <tr class=""aleft"">" & vbcrlf 
   doc.write "        <th>" & SDB.Localize("Artist") & "</th>" & vbcrlf  
   doc.write "        <th>" & SDB.Localize("Album Artist") & "</th>" & vbcrlf 
   doc.write "      </tr>" & vbcrlf 

   Dim i, j, itm, art, alb
   Dim artist, albumArtist 
   For i = 0 To trackList.Count-1 
      doc.write "      <tr" & Style() & ">" & vbcrlf 
      Set itm = trackList.Item(i) 
      artist = ""
      albumArtist = ""
      
      art = Split(itm.artistName,"; ")
      For j = 0 To UBound(art)
        Select Case mode
          Case 0
            If artist = "" Then
              artist = rename(art(j)) 
            Else
              artist = artist&"; "&rename(art(j)) 
            End If
          Case 1
            If artist = "" Then
              artist = rename2(art(j)) 
            Else
              artist = artist&"; "&rename2(art(j)) 
            End If
          Case Else
            If artist = "" Then
              artist = art(j)
            Else
              artist = artist&"; "&art(j)
            End If
        End Select
      Next
      
      alb = Split(itm.albumArtistName,"; ")
      For j = 0 To UBound(alb)
        Select Case mode
          Case 0
            If albumArtist = "" Then
              albumArtist = rename(alb(j)) 
            Else
              albumArtist = albumArtist&"; "&rename(alb(j)) 
            End If
          Case 1
            If albumArtist = "" Then
              albumArtist = rename2(alb(j)) 
            Else
              albumArtist = albumArtist&"; "&rename2(alb(j)) 
            End If
          Case Else
            If albumArtist = "" Then
              albumArtist = alb(j)
            Else
              albumArtist = albumArtist&"; "&alb(j)
            End If
        End Select
      Next
      
      doc.write outField(artist, itm.artistName) 
      doc.write outField(albumArtist, itm.albumArtistName) 
      If artist <> "" And artist <> itm.artistName Then 
         holdArtist.add itm, artist 
      End If 
      If albumArtist <> "" And albumArtist <> itm.albumArtistName Then 
         holdAlbumArtist.add itm, albumArtist 
      End If 
      doc.write "      </tr>" & vbcrlf 
       
   next 'i 
    
   doc.write "    </table>" & vbcrlf 
   doc.write "  </body>" & vbcrlf 
   doc.write "</html>" & vbcrlf 
   doc.close 
   SDB.Objects("holdArtist") = holdArtist 
   SDB.Objects("holdAlbumArtist") = holdAlbumArtist 
End Sub 

Sub Install()
  Dim inip : inip = SDB.ApplicationPath&"Scripts\Scripts.ini"
  Dim inif : Set inif = SDB.Tools.IniFileByPath(inip)
  If Not (inif Is Nothing) Then
    inif.StringValue("AdvancedRename","Filename") = "AdvancedRename.vbs"
    inif.StringValue("AdvancedRename","Procname") = "AdvancedRename"
    inif.StringValue("AdvancedRename","Order") = "14"
    inif.StringValue("AdvancedRename","DisplayName") = "Advanced Rename"
    inif.StringValue("AdvancedRename","Description") = "Firstname Lastname -> Lastname, Firstname"
    inif.StringValue("AdvancedRename","Language") = "VBScript"
    inif.StringValue("AdvancedRename","ScriptType") = "0"
    SDB.RefreshScriptItems
  End If
End Sub
Last edited by trixmoto on Mon Nov 12, 2007 11:43 am, edited 3 times in total.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

:o NEW VERSION BELOW :o
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

New version (2.0) is now available to download from my website.

This version has the reverse option available, although the prefixes and postfixes are ignored. You need to open the installed script file in a text editor and change the "mode" variable to 1 (instead of 0) to use the new mode.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
kdeemer
Posts: 81
Joined: Tue Jul 11, 2006 11:51 pm
Location: Los Angeles

Post by kdeemer »

This is great, but it doesn't change the Album Artist, so you need to do this manually (or am I missing something?)
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

There's a bug with MM where the Album Artist doesn't update properly from scripts. Hopefully this will be fixed in a future version and then this script will work properly.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Nebbin
Posts: 316
Joined: Mon May 30, 2005 4:52 am
Location: Australia

Post by Nebbin »

*phew* all this time I thought it was just me :(

This script, the Case Checker script and the Modify Field script (part of the Custom Scripts assortment) weren't updating the Album Artist field properly and I've been searching high and low for a solution, including reinstalling windows from scratch! lol...

... and all it could have taken was a simply query in these forums? D'Oh! :oops:
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

Reinstalling windows!? Now that's a drastic measure! :lol:
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Bex
Posts: 6316
Joined: Fri May 21, 2004 5:44 am
Location: Sweden

Post by Bex »

trixmoto wrote:There's a bug with MM where the Album Artist doesn't update properly from scripts. Hopefully this will be fixed in a future version and then this script will work properly.
@ MM-Team!
It would be nice if this bug could be resolved. It's rather annoying not to have the album artist field updated properly in various scripts.

Thanks
/Bex
Advanced Duplicate Find & Fix Find More From Same - Custom Search. | Transfer PlayStat & Copy-Paste Tags/AlbumArt between any tracks.
Tagging Inconsistencies Do you think you have your tags in order? Think again...
Play History & Stats Node Like having your Last-FM account stored locally, but more advanced.
Case & Leading Zero Fixer Works on filenames too!

All My Scripts
Sino

Post by Sino »

Could you PLEASE, PLEASE make it work without the comma? I've got some 50 or so collections of songs that are in the format "Lastname Firstname" that i want to convert to "Firstname Lastname" that i normally use. It messes up my database...

Please? :D

-Sino.
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

You can make this change yourself by opening the script in a text editor and making the following small changes...

EDIT: download the newest release!
Last edited by trixmoto on Sun Oct 15, 2006 7:22 am, edited 1 time in total.
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
Sino

Post by Sino »

I was really willing to buy you the next pint mate, but then again -- it says that i still can't do my job because there's "no comma, various" [that's the error message]. I guess it means that i've used "Various" as the Album Artist for my collections [something i do in order for MediaMonkey to understand that these artists didn't do the entire album but just a track of the album]. So the script depends on Album Artist instead of Artist?

Any chance of making things work and you having the next pint on me? :D

-Sino.
psyXonova
Posts: 785
Joined: Fri May 20, 2005 3:57 am
Location: Nicosia, Cyprus
Contact:

Post by psyXonova »

trixmoto wrote:There's a bug with MM where the Album Artist doesn't update properly from scripts. Hopefully this will be fixed in a future version and then this script will work properly.
Most probably i am posting this to the wrong place but Case Checker works fine with me (meaning it also updates the Album Artist field, and yes after i restart MM correct Album Artist is still there)..
trixmoto
Posts: 10024
Joined: Fri Aug 26, 2005 3:28 am
Location: Hull, UK
Contact:

Post by trixmoto »

New version (2.1) can now be downloaded from my website. This version does not contain the annoying (and rather useless) messages, just the confirmation screen. There is also a new delimiter variable.

@Sino - you'll want to change this to delim = " ".

@psyxonova - this script was originally copied from Risser's "Title Case" script - is this the one you're referring to?
Download my scripts at my own MediaMonkey fansite.
All the code for my website and scripts is safely backed up immediately and for free using Dropbox.
psyXonova
Posts: 785
Joined: Fri May 20, 2005 3:57 am
Location: Nicosia, Cyprus
Contact:

Post by psyXonova »

trixmoto wrote:@psyxonova - this script was originally copied from Risser's "Title Case" script - is this the one you're referring to?
Yes, thats the one... although you have to open the vbs file to see its name as "Title Case" (or did i changed it sometime in the past and cant remember it??? Seems like I lost several valuable neurons since I moved to Cyprus... must be the clean air....)
Montana
Posts: 4
Joined: Sun Oct 22, 2006 8:50 pm
Contact:

Post by Montana »

I've just recently started putzing around with the scripts - so forgive any n00bocity.

However, all of Trix's scripts gave me the ol' blank window. I use a combination of the latest MM and IE 7, so apparently this doesn't seem to like "document.write". By looking at the "newer" version of what the script writer used for their base - the Case.vbs script that now comes bundled with MM - I saw that simple string concatenation was now being used to aggregate the HTML window output.

So, I just went through and changed document.write instances to accumulate into a string called HTML, then used "WB.SetHTMLDocument(HTML) instead of "doc.close" and it started working for me again. I could see the contents of the dialog window!

As a note, I was simply mimicking what the Case.vbs author already does! Not being original!

Well, that solved one problem. Problem #2 is that the changes don't seem to "stick". I can update onscreen, but as soon as I navigate away on the node tree and come back, the fields would be back to their original state.

This goes some of the other scripts out there too, like AdvanceRename. I think the culprit is just that Trix was only calling "itm.UpdateDB" instead of "

itm.UpdateArtist
itm.UpdateAlbum
itm.UpdateDB
"

... like I've seen in some other scripts out there. This may be an unnecessary redundancy though. I need to confirm that this works still!
Post Reply