Page 1 of 3

[MM2+3] Update Custom Fields with Pre-Selection

Posted: Mon Jan 16, 2006 1:28 am
by onkel_enno
Here is a small script to edit the Custom Fields.
Create a File EditCustomX.vbs in the MM\Scripts\Auto Folder and paste the code from below!
It will create SubMenus in most Pop-Ups.

This example is for adding/removing the Language to the CustomField 2. (changeable in line 6)
When Changing the Constant OnlyOneSelection (line 7) to False, it is possible to Add more than One Language to one Song or, like in the Picture, more than one Keyword.

Customize it for your Needs!

Image

Code: Select all

Option Explicit
'#################################################
'           User-defined Settings
'#################################################

const CustomField = 2         'which Custom Field should be edited MM2: 1..3; MM3: 1..5
const OnlyOneSelection = True 'should the Selection be removed/added to the custom field or should the field be replaced/cleared

Dim dictCustomEntries
Set dictCustomEntries = CreateObject("Scripting.Dictionary")
dictCustomEntries.Add 1, "deutsch"
dictCustomEntries.Add 2, "englisch"
dictCustomEntries.Add 3, "französisch"
dictCustomEntries.Add 4, "finnisch"
dictCustomEntries.Add 5, "italienisch"
dictCustomEntries.Add 6, "japanisch"
dictCustomEntries.Add 7, "spanisch"

'#################################################
'   Start-Up Function (Create Menu Icons)
'#################################################

Dim objMenu
Dim iniField
objMenu = "mnuCustom" & CustomField
iniField = "Fld" & CustomField & "Name"

Sub OnStartUp
	Dim MenuItem
	Dim Typ
	
	Typ = "Tray"
	set MenuItem = SDB.Objects(objMenu & Typ)
		if MenuItem is nothing then
			set MenuItem = SDB.UI.AddMenuItemSub(SDB.UI.Menu_TrayIcon, 0, 0)
			MenuItem.Caption = SDB.INIFile.StringValue("CustomFields", iniField)
			MenuItem.Hint = Typ
			MenuItem.Enabled = True
			MenuItem.UseScript = Script.ScriptPath
			MenuItem.OnClickFunc = "AddSubMenuItems"
			SDB.Objects(objMenu) = MenuItem
			AddSubMenuItems(MenuItem)
		end if
		
	Typ = "NP"
	set MenuItem = SDB.Objects(objMenu & Typ)
		if MenuItem is nothing then
			set MenuItem = SDB.UI.AddMenuItemSub(SDB.UI.Menu_Pop_NP, 0, -1)
			MenuItem.Caption = SDB.INIFile.StringValue("CustomFields", iniField)
			MenuItem.Hint = Typ
			MenuItem.Enabled = True
			MenuItem.UseScript = Script.ScriptPath
			MenuItem.OnClickFunc = "AddSubMenuItems"
			SDB.Objects(objMenu) = MenuItem
			AddSubMenuItems(MenuItem)
		end if
		
	Typ = "Main"
	set MenuItem = SDB.Objects(objMenu & Typ)
		if MenuItem is nothing then
			set MenuItem = SDB.UI.AddMenuItemSub(SDB.UI.Menu_Pop_TrackList, 0, -1)
			MenuItem.Caption = SDB.INIFile.StringValue("CustomFields", iniField)
			MenuItem.Hint = Typ
			MenuItem.Enabled = True
			MenuItem.UseScript = Script.ScriptPath
			MenuItem.OnClickFunc = "AddSubMenuItems"
			SDB.Objects(objMenu) = MenuItem
			AddSubMenuItems(MenuItem)
		end if
		
	Set MenuItem = Nothing
end Sub

'#################################################
'          Add Sub-Menus (Custom Entries)
'#################################################

Sub AddSubMenuItems(Node)
	Dim a
	Dim SubEntry
	Dim Track
	Dim Entry
	Dim ObjName
	Dim Typ
	Typ = Node.Hint

	for a = 1 to dictCustomEntries.Count
		ObjName = objMenu & Typ & a
		set SubEntry = SDB.Objects(ObjName)
			if SubEntry is Nothing then
				set SubEntry = SDB.UI.AddMenuItem(Node, 1, -1)
				'MenuItem.IconIndex = 56
				SubEntry.Caption = dictCustomEntries.Item(a)
				SubEntry.Hint = Typ
				SubEntry.Enabled = True
				SubEntry.UseScript = Script.ScriptPath
				SubEntry.OnClickFunc = "SetCustom"
				SDB.Objects(ObjName) = SubEntry		
			end if
			
			Dim Tracks
			if Typ = "NP" then
				Set Tracks = SDB.SelectedSongList
			elseif Typ = "Main" then
				Set Tracks = SDB.SelectedSongList
			elseif Typ = "Tray" then
				Set Tracks = SDB.NewSongList
				if not (SDB.Player.CurrentSong is nothing) then Tracks.Add(SDB.Player.CurrentSong)
			end if
			
			if Tracks.Count = 1 then
				Set Track = Tracks.Item(0)
				Select case CustomField
					Case 1
						Entry = Track.Custom1
					Case 2
						Entry = Track.Custom2
					Case 3
						Entry = Track.Custom3
					Case 4
						Entry = Track.Custom4
					Case 5
						Entry = Track.Custom5
				end select
				SubEntry.Checked = Instr(1, Entry, dictCustomEntries.Item(a), 1)
				Set Track = Nothing
			else
				SubEntry.Checked = False
			end if
		Set SubEntry = Nothing
	next
End Sub

'#################################################
'          Change Custom Entries
'#################################################

Sub SetCustom(Node)
	Dim Entry
	Dim Typ
	
	Typ = Node.Hint
	Dim Tracks
	if Typ = "NP" then
		Set Tracks = SDB.SelectedSongList
	elseif Typ = "Main" then
		Set Tracks = SDB.SelectedSongList
	elseif Typ = "Tray" then
		Set Tracks = SDB.NewSongList
		if not (SDB.Player.CurrentSong is nothing) then Tracks.Add(SDB.Player.CurrentSong)
	end if
	
	Dim Track
	Dim a
	for a = 0 to Tracks.Count -1
		Set Track = Tracks.Item(a)
		
		Select Case CustomField
			Case 1
				Entry = Track.Custom1
			Case 2
				Entry = Track.Custom2
			Case 3
				Entry = Track.Custom3
			Case 4
				Entry = Track.Custom4
			Case 5
				Entry = Track.Custom5
		end Select
		
		if OnlyOneSelection then
			if LCase(Entry) = LCase(Node.Caption) then
				Entry = ""
			else
				Entry = Node.Caption
			end if
		else
			if InStr(1, Entry, Node.Caption, 1) then
				'remove KeyWord
				while InStr(1, Entry, Node.Caption, 1)
					Entry = Replace(Entry, Node.Caption, "", 1, -1, 1)
				Wend
				
				'clean String
				while InStr(1, Entry, "  ", 1)
					Entry = Replace(Entry, "  ", " ", 1, -1, 1)
				Wend
				while InStr(1, Entry, ", ,", 1)
					Entry = Replace(Entry, ", ,", ",", 1, -1, 1)
				Wend
				while InStr(1, Entry, ",,", 1)
					Entry = Replace(Entry, ",,", ",", 1, -1, 1)
				Wend
			else
				'Add KeyWord
				if Entry <> "" then Entry = Entry & ", "
				Entry = Entry & Node.Caption
			end if
			
			'Clean string
			Entry = Trim(Entry)
			While Right(Entry, 1) = "," 
				Entry = Left(Entry, Len(Entry)-1)
				Entry = Trim(Entry)
			Wend
			While Left(Entry, 1) = ","
				Entry = Mid(Entry, 2)
				Entry = Trim(Entry)
			Wend
		end if
		
		Select Case CustomField
			Case 1
				Track.Custom1 = Entry
			Case 2
				Track.Custom2 = Entry
			Case 3
				Track.Custom3 = Entry
			Case 4
				Track.Custom4 = Entry
			Case 5
				Track.Custom5 = Entry
		end Select
		
		Track.UpdateDB
		
		Set Track = Nothing
	next
end Sub

Re: Auto\Scripts:Editing Custom Fields with Pre-Selection

Posted: Mon Jan 23, 2006 6:10 am
by Paven
onkel_enno wrote:Here is a small script to edit the Custom Fields.

Code: Select all

         elseif Typ = "Tray" then
            Set Tracks = SDB.NewSongList
            Tracks.Add(SDB.Player.CurrentSong) 
         end if
I get an erro message at line 108 colum 12

Code: Select all

            Tracks.Add(SDB.Player.CurrentSong) 
i removed the line and got ride of the error message, but would like to get back that function...
anny solutions?

Posted: Mon Jan 23, 2006 6:33 am
by onkel_enno
That happens when no track is selected in Player - never happens to me :lol:

Please add a

Code: Select all

if not (SDB.Player.CurrentSong is nothing) then 
in front of line 108 and 146 (it should be) so that it looks like

Code: Select all

...
			Dim Tracks
			if Typ = "NP" then
				Set Tracks = SDB.SelectedSongList
			elseif Typ = "Main" then
				Set Tracks = SDB.SelectedSongList
			elseif Typ = "Tray" then
				Set Tracks = SDB.NewSongList
				if not (SDB.Player.CurrentSong is nothing) then Tracks.Add(SDB.Player.CurrentSong)
			end if

...	

	Typ = Node.Hint
	Dim Tracks
	if Typ = "NP" then
		Set Tracks = SDB.SelectedSongList
	elseif Typ = "Main" then
		Set Tracks = SDB.SelectedSongList
	elseif Typ = "Tray" then
		Set Tracks = SDB.NewSongList
		if not (SDB.Player.CurrentSong is nothing) then Tracks.Add(SDB.Player.CurrentSong)
	end if

or copy the complete Code from above again.

Posted: Mon Jan 23, 2006 7:34 am
by Paven
Thanx that worked well

Posted: Mon Jan 23, 2006 9:09 am
by Paven
I have an idee of how to make this script even bether but don't know how to do it.

I would like the script to get a list of whats posible to select from whats already existis in other tracks. Making this script more dynamic.

(perhaps limited to 10 options sorted by mosted used.)

perhaps i could get som tip on how to start. :)

Regards
Patrik

Posted: Mon Jan 23, 2006 9:21 am
by onkel_enno
Paven wrote: perhaps i could get som tip on how to start. :)
OK. :lol:
You will have to create the dictionary in another way.
Use "OpenSQL" to create a query with your selection of Keywords.
For example

Code: Select all

SELECT Custom2 FROM Songs GROUP BY Custom2
HAVING Custom2<>"" ORDER BY Count(Custom2) DESC;
But be careful. The Dictionary I created doesn't change while MM is running.
A Query might give different results, each time you call the script.

basic error?

Posted: Mon Jan 23, 2006 10:25 am
by Paven
I started on the script... but got an error object requird error message nad i can't figure out why. the error is at this line.

Code: Select all

Set sql = "SELECT Custom" _
is ther som basic basic rule that i have missed?

Code: Select all

 Oconst CustomField = 1         'which Custom Field should be edited
const OnlyOneSelection = True 'should the Selection be removed/added to the custom field or should the field be replaced/cleared
const DynamicDict = True

Dim dictCustomEntries
Set dictCustomEntries = CreateObject("Scripting.Dictionary")

If DynamicDict = False Then
   dictCustomEntries.Add 1, "MUSIC"
   dictCustomEntries.Add 2, "BOOK"
   dictCustomEntries.Add 3, "SPOKEN"
   dictCustomEntries.Add 4, "BETA"
   dictCustomEntries.Add 5, ""
End If
If DynamicDict = True Then
  dim sql
  Set sql = "SELECT Custom" _
    & CustomField _
    & " FROM Songs GROUP BY Custom" _
    & CustomField _
    & " ORDER BY Count(Custom" _
    & CustomField _
    & ") DESC;"
  SDB.database.execSQL(sql)
  dictCustomEntries = SDB.Database.OpenSQL(sql) 
End If

Posted: Tue Jan 24, 2006 1:10 am
by onkel_enno
"sql" is a string.
Use sql = "..." instead of set sql =

Tip: You can use the Visual-Basic-Editor of Microsoft Word/Excel/Access/... to check basic VB-Syntax.

Thnx

Posted: Sat Jan 28, 2006 11:07 pm
by CathM
This is exactly what I was looking for. Even the use of the custom fields is almost the same.

Thanx!!!!

Posted: Mon Jan 30, 2006 1:23 am
by onkel_enno
Nice to read :D

Posted: Mon Jan 15, 2007 6:10 am
by moehesse
I have missed this function for a long time, discovered your script first today. It is wonderful! Thanks!

Re: basic error?

Posted: Mon Jan 15, 2007 9:50 am
by RedX
Paven wrote:I started on the script... but got an error object requird error message nad i can't figure out why. the error is at this line.

Code: Select all

Set sql = "SELECT Custom" _
is ther som basic basic rule that i have missed?

Code: Select all

 Oconst CustomField = 1         'which Custom Field should be edited
const OnlyOneSelection = True 'should the Selection be removed/added to  the custom field or should the field be replaced/cleared
const DynamicDict = True

Dim dictCustomEntries
Set dictCustomEntries = CreateObject("Scripting.Dictionary")

If DynamicDict = False Then
   dictCustomEntries.Add 1, "MUSIC"
   dictCustomEntries.Add 2, "BOOK"
   dictCustomEntries.Add 3, "SPOKEN"
   dictCustomEntries.Add 4, "BETA"
   dictCustomEntries.Add 5, ""
End If
If DynamicDict = True Then
  dim sql
  Set sql = "SELECT Custom" _
    & CustomField _
    & " FROM Songs GROUP BY Custom" _
    & CustomField _
    & " ORDER BY Count(Custom" _
    & CustomField _
    & ") DESC;"
  SDB.database.execSQL(sql)
  dictCustomEntries = SDB.Database.OpenSQL(sql) 
End If
corrected but not tried out:

Code: Select all

 Oconst CustomField = 1         'which Custom Field should be edited
const OnlyOneSelection = True 'should the Selection be removed/added to the custom field or should the field be replaced/cleared
const DynamicDict = True

Dim dictCustomEntries
Set dictCustomEntries = CreateObject("Scripting.Dictionary")

If DynamicDict = False Then
   dictCustomEntries.Add 1, "MUSIC"
   dictCustomEntries.Add 2, "BOOK"
   dictCustomEntries.Add 3, "SPOKEN"
   dictCustomEntries.Add 4, "BETA"
   dictCustomEntries.Add 5, ""
End If
If DynamicDict = True Then
  dim sql
  sql = "SELECT Custom"  _
    & CustomField _
    & " FROM Songs GROUP BY Custom" _
    & CustomField _
    & " ORDER BY Count(Custom" _
    & CustomField _
    & ") DESC;"
  
  dictCustomEntries = SDB.Database.OpenSQL(sql) 
End If

Posted: Wed Jan 17, 2007 4:11 pm
by MeMeMe
Do you just create two versions of this script if you want it to modify custm2 and custom3?

Posted: Thu Jan 18, 2007 12:53 am
by onkel_enno
Correct!

Posted: Thu Jan 18, 2007 9:43 am
by MeMeMe
Great!
And thanks, this looks like the script I've wanted for a while, to keep track of multiple genres assigned to one song.