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

Post a reply

Smilies
:D :) :( :o :-? 8) :lol: :x :P :oops: :cry: :evil: :roll: :wink:

BBCode is ON
[img] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: I'm working on a script to prevent typos in custom fields

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

by kp1 » Sun Jan 15, 2017 6:59 pm

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

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

by kp1 » Sun Jan 15, 2017 6:02 pm

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:

GetFieldValues("Custom2")

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

FieldValues_Custom3.vbs:

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
		Next
	Next
	
	'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
	Next
	
	'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
	Next
	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
		Next
	Next
	SortArray = arrTemp
End Function
Added to Scripts.ini:

Code: Select all

[FieldValues_Custom3]
FileName=FieldValues_Custom3.vbs
ProcName=FieldValues_Custom3
Order=60
DisplayName=List Field Values: Instrumention
Description=Shows Instrumentation values for all songs in the file list
Language=VBScript
ScriptType=0

Top