"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
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