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

Download and get help for different MediaMonkey Addons.

Moderators: Peke, Gurus

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

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

Post by onkel_enno » Mon Jan 16, 2006 1:28 am

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
Last edited by onkel_enno on Mon Apr 28, 2008 3:25 am, edited 12 times in total.
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

Paven
Posts: 37
Joined: Mon Jan 23, 2006 5:58 am
Location: Sweden

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

Post by Paven » Mon Jan 23, 2006 6:10 am

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?

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

Post by onkel_enno » Mon Jan 23, 2006 6:33 am

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.
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

Paven
Posts: 37
Joined: Mon Jan 23, 2006 5:58 am
Location: Sweden

Post by Paven » Mon Jan 23, 2006 7:34 am

Thanx that worked well

Paven
Posts: 37
Joined: Mon Jan 23, 2006 5:58 am
Location: Sweden

Post by Paven » Mon Jan 23, 2006 9:09 am

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

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

Post by onkel_enno » Mon Jan 23, 2006 9:21 am

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.
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

Paven
Posts: 37
Joined: Mon Jan 23, 2006 5:58 am
Location: Sweden

basic error?

Post by Paven » Mon Jan 23, 2006 10:25 am

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

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

Post by onkel_enno » Tue Jan 24, 2006 1:10 am

"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.
Last edited by onkel_enno on Mon Jan 30, 2006 3:25 am, edited 1 time in total.
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

CathM

Thnx

Post by CathM » Sat Jan 28, 2006 11:07 pm

This is exactly what I was looking for. Even the use of the custom fields is almost the same.

Thanx!!!!

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

Post by onkel_enno » Mon Jan 30, 2006 1:23 am

Nice to read :D
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

moehesse
Posts: 15
Joined: Sat Dec 31, 2005 5:15 am

Post by moehesse » Mon Jan 15, 2007 6:10 am

I have missed this function for a long time, discovered your script first today. It is wonderful! Thanks!

RedX
Posts: 366
Joined: Wed Dec 27, 2006 10:32 am
Location: Germany

Re: basic error?

Post by RedX » Mon Jan 15, 2007 9:50 am

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

MeMeMe
Posts: 263
Joined: Fri Dec 23, 2005 11:42 am
Location: In front of my computer

Post by MeMeMe » Wed Jan 17, 2007 4:11 pm

Do you just create two versions of this script if you want it to modify custm2 and custom3?

onkel_enno
Posts: 2146
Joined: Fri Jan 14, 2005 1:45 am
Location: Germany
Contact:

Post by onkel_enno » Thu Jan 18, 2007 12:53 am

Correct!
SansaMonkey - for SanDisk Sansa and Rockbox Users

Please no PMs for Questions which should be asked in the Forum. Thx

MeMeMe
Posts: 263
Joined: Fri Dec 23, 2005 11:42 am
Location: In front of my computer

Post by MeMeMe » Thu Jan 18, 2007 9:43 am

Great!
And thanks, this looks like the script I've wanted for a while, to keep track of multiple genres assigned to one song.

Post Reply