I'm working on a script to prevent typos in custom fields

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

Posts: 8
Joined: Sun Jan 15, 2017 5:08 pm

I'm working on a script to prevent typos in custom fields

Post by kp1 »

I like to use delimited strings in my custom fields, for instance I'm using the Custom3 field right now as an "Instrumentation" field, where I might type something like
"Piano; Chorus; Violin".

The problem with this is that there's no autocomplete feature for these custom fields. Eventually I want to be able to use this script to input mutliple values into any field by selecting from a list of existing values.

Right now this script just looks through the visible song list, searches through all the values in the Custom3 fields, splits up delimited strings, and then displays all the unique values in a listbox.

The problem that I'm having right now is how to make this script more generalized, so instead of having a bunch of duplicated code for each different field name, I could just call a single function with the field name as a parameter, like:


However, the only way I've figured out how to do this is to use the eval function:

fieldValue = eval("songList.item(i)." & FIELD_NAME)

...which is extremely slow.
If I hardcode it like this, the script is nearly instantaneous:

fieldValue = songList.item(i).Custom3


Code: Select all

'Version: 2017.01.13
'Description: Creates a listbox of all delimited values in the Custom3 Field

Sub FieldValues_Custom3()
	const FIELD_NAME = "Custom3"
	const FORM_WIDTH = 400
	const FORM_HEIGHT = 400
	const DELIMITER = ";"
	'Create dictionary of unique field values
	dim songList: Set songList = SDB.AllVisibleSongList 
	dim dictFieldVals: Set dictFieldVals = CreateObject("Scripting.Dictionary")
	dim i, fieldValue, fieldItems, fieldItem
	For i=0 To songList.count-1
		fieldValue = songList.item(i).Custom3 'Faster, but field name is hard-coded
		'fieldValue = eval("songList.item(i)." & FIELD_NAME) 'Much slower, but field name is variable
		fieldItems = split(fieldValue,DELIMITER)
		For Each fieldItem in fieldItems
			fieldItem = trim(fieldItem)
			dictFieldVals(fieldItem) = fieldItem
	'Create form
	Dim oForm: Set oForm = SDB.UI.NewForm
	With oForm
		.Common.SetRect 100, 100, FORM_WIDTH, FORM_HEIGHT
		.BorderStyle = 3
		.FormPosition = 4
		.StayOnTop = True
		.Caption = FIELD_NAME & " Field Values"
	End with

	'Create listbox
	Dim oListBox: Set oListBox = SDB.UI.NewListBox(oForm)
	oListBox.Common.SetRect 0, 0, FORM_WIDTH-20, FORM_HEIGHT-40
	'Populate listbox
	For each fieldValue in SortArray(DictToArray(dictFieldVals))
		oListBox.Items.Add fieldValue
	'Show form
	oForm.Common.Visible = True
	SDB.Objects("oForm") = oForm
End Sub

Function DictToArray(objDict) 
	Dim nCount, strKey, arrTemp
	Redim arrTemp(objDict.Count - 1)

	i = 0
	For Each strKey In objDict.Keys
		arrTemp(i) = strKey 
		i = i + 1
	DictToArray = arrTemp
End Function

Function SortArray(arrTemp) 
	Dim iTemp, jTemp, strTemp
	For iTemp = 0 To UBound(arrTemp)  
		For jTemp = 0 To iTemp
		If strComp(arrTemp(jTemp), arrTemp(iTemp),1) > 0 Then
			strTemp = arrTemp(jTemp)
			arrTemp(jTemp) = arrTemp(iTemp)
			arrTemp(iTemp) = strTemp
		End If
	SortArray = arrTemp
End Function
Added to Scripts.ini:

Code: Select all

DisplayName=List Field Values: Instrumention
Description=Shows Instrumentation values for all songs in the file list
Posts: 8
Joined: Sun Jan 15, 2017 5:08 pm

Re: I'm working on a script to prevent typos in custom field

Post by kp1 »

Nevermind, I found something that works pretty well. I just had to modify this script a bit:
http://www.mediamonkey.com/forum/viewto ... =2&t=58583
Post Reply