Need help with script to set mp3 tag from partial filename

I have mp3 files in folders which are named in this format:
nn.xxxx.mp3
...where nn is the track number (may be 1, 2 or 3 digits long) and xxxx is the track name.
I want to write a VBScript (triggered by a button) to process all mp3 files in the current folder and set the Track Number ID3 tag to the track number from the start of the filename (i.e. the bit before the ".").
This is what I have so far:

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, selItem, tracknum
	If clickData.func.sourcetab.selected.count > 0 Then
		For Each selItem in clickData.func.sourcetab.selected
				tracknum = selItem.name_stem.Substring(0, selItem.name_stem.IndexOf("."))
				'COMMAND HERE TO SET MP3 TAG TO tracknum!!'
		Next
	End If

I want to replace the 'COMMAND HERE TO SET MP3 TAG TO tracknum!!' line with the relevant command to do this. Can anyone advise what this command should be, or have I gone about this the wrong way?

You are looking for SetAttr META.

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Programmatic_setting_of_Metadata.htm

Are you mixing VBS and JS?

Thanks for the pointer. How do I get the SetAttr META command to act on the selected item only in the loop (rather than applying the same change to all of the files in the selection)?

For each file, call ClearFiles on the command object you're using to clear any existing files inside it, then use AddFile to add just the file you want to affect to it.

Thanks. So the code below now produces this error:

Error at line 8, position 5
Object required: '[string: "06.The Ballad of Joh"]' (0x800a01a8)

(the first file is named "06.The Ballad of John Henry")

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, selItem, tracknum, strCommand
	Dim myfilename, dlg
	Set cmd = clickData.func.command
	If clickData.func.sourcetab.selected.count > 0 Then
		For Each selItem in clickData.func.sourcetab.selected
				Set myfilename = selItem.name_stem
				Set tracknum = myfilename.Substring(0, myfilename.IndexOf("."))
				cmd.ClearFiles
				cmd.AddFile(selItem)
				strCommand = "SetAttr META track:" & tracknum
				Shell.Run strCommand
		Next
	End If
End Function

Any idea why I get the "Object required" error here? (line 8 is "Set myfilename = selItem.name_stem")

Item.name_stem returns a string. The Set keyword in VBScript is only used for objects.

If I remove the Set to leave this:
myfilename = selItem.name_stem
I still get this error on that line:

Error at line 9, position 5
Object required: 'myfilename' (0x800a01a8)

The error is coming from the next line, which looks like it was from JScript. VBScript has functions like SubStr(...), not object methods like x.Substring(...)

Thanks. OK I finally managed to find some syntax which got past the parser (DOpus doesn't seem to like nested function calls)....
So the code below runs, but does not produce the expected result. I have 10 files selected in a folder when I run the script. They are numbered from 01 to 10 in the prefix of the filename. The script assigns a track number of "02" to the first 5 tracks, and a track number of "06" to the last 5 tracks.
As there's no debug function I can't work out where the logic is breaking down.
Any idea what's wrong?

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, selItem, tracknum, strCommand
	Dim myfilename, dlg, dotPos, Shell
	Dim DOpusRTPath
	DOpusRTPath = "C:\Program Files\GPSoftware\Directory Opus\dopusrt.exe"
	Set cmd = clickData.func.command
	Set Shell = CreateObject("WScript.Shell")
	If clickData.func.sourcetab.selected.count > 0 Then
		For Each selItem in clickData.func.sourcetab.selected
				myfilename = selItem.name_stem
				dotPos = Instr(myfilename,".")
				dotPos = dotPos - 1
				If dotPos > 0 Then
					tracknum = Left(myfilename,dotPos)
				Else
					tracknum = "00"
				End If
				cmd.ClearFiles
				cmd.AddFile(selItem)
				strCommand = """" & DOpusRTPath & """ /cmd SetAttr META ""track:" & tracknum
				Shell.Run strCommand
		Next
	End If
End Function

Opus isn't involved in the parsing. VBScript is part of Windows.

You can use the DOpus.Output function to print things to the script log when you need to debug which commands etc. your script is running.

There's no need to use dopusrt for this, by the way. In fact, that is what's causing your problem. If you run the command via DOpusRT.exe, it's going to run that command independently of your script and the command will run on all files, not just the ones in your script's command object. Remove all the DOpusRT stuff and run the SetAttr command directly from the script.

OK I now have:
SetAttr META "tracknumber:" & tracknum

...but it gives this error:

SetAttr META "tracknumber:" & tracknum
             ^
Expected end of statement (0x800a0401)
Parse error - script aborted

Here's the full script:

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, selItem, tracknum, strCommand
	Dim myfilename, dlg, dotPos, Shell
	Set cmd = clickData.func.command
	Set Shell = CreateObject("WScript.Shell")
	If clickData.func.sourcetab.selected.count > 0 Then
		For Each selItem in clickData.func.sourcetab.selected
				myfilename = selItem.name_stem
				dotPos = Instr(myfilename,".")
				dotPos = dotPos - 1
				If dotPos > 0 Then
					tracknum = Left(myfilename,dotPos)
				Else
					tracknum = "00"
				End If
				cmd.ClearFiles
				cmd.AddFile(selItem)
				SetAttr META "tracknumber:" & tracknum
		Next
	End If
End Function

???

SetAttr is an Opus command, not a scripting function or object. Opus command should be run via the Opus command object, cmd in your script. The same object you add the files to is the one that should be used to run the command.

Have you looked at any other scripts to see how they run commands? I would start there. The default script you get in the button editor has an example where it runs Set VIEW=Details.

OK I've added logging and am trying to use the command object, based on examples elsewhere.
I now get this error:

Error at line 23, position 5
A method was called unexpectedly (0x8000ffff)

Line 23 is DOpus.Output = "strCommand =" & strCommand

I also see nothing in the Script Output console apart from this error message (i.e. no "Hello World" message).

Option Explicit
Function OnClick(ByRef clickData)
	Dim cmd, selItem, tracknum, strCommand
	Dim myfilename, dlg, dotPos, Shell
	Set cmd = clickData.func.command
	Set Shell = CreateObject("WScript.Shell")
	cmd.RunCommand("Set UTILITY=otherlog")
	DOpus.Output("Hello, World in the Output Log")
	DOpus.ClearOutput()
	If clickData.func.sourcetab.selected.count > 0 Then
		For Each selItem in clickData.func.sourcetab.selected
				myfilename = selItem.name_stem
				dotPos = Instr(myfilename,".")
				dotPos = dotPos - 1
				If dotPos > 0 Then
					tracknum = Left(myfilename,dotPos)
				Else
					tracknum = "00"
				End If
				cmd.ClearFiles
				cmd.AddFile(selItem)
				strCommand = "SetAttr META tracknumber:" & tracknum
				DOpus.Output = "strCommand =" & strCommand
				cmd.RunCommand(strCommand)
		Next
	End If
End Function

If I remove the offending Dopus.Output line the script will run. I see nothing in the Script Output window, but an error like this pops up in a dialog box for every file/iteration (and the tracknumber tags are not modified):
"An error occurred setting metadata '01.I Know Where I Belong.mp3'; Metadata parsing error.

DOpus.Output is a method/function. You were trying to assign to it like it was a value.

Please link your account if you need more help, especially if you want us to teach basic scripting/syntax that is outside the scope of Opus itself.

Needs to be track instead of tracknumber. Looks like the example in the docs is wrong.

Ah...that was it - thanks! (track rather than tracknumber). I was using an example from this page:
https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Keywords_for_SetAttr_META.htm

Did I misunderstand the example, or is it a misprint?

I've fixed that. (Takes a while for manual changes to reach the web copy, though.)

The keyword column itself was correct but the example for how to use it was wrong.