Mp3 Tag File (Built)

reso is an AudioCoverArt object that provides the size property which returns a FileSize object. This object's fmt property looks useful.

Putting it all together: reso.size.fmt.

1 Like

Note :100:, I had already seen fmt in many scripts but I never thought it would make that big of a difference, grateful for more!

I can't believe it, the script is almost ready, now I just assign the functions to the buttons.

I would really like to understand how these events work, for example, I have a "btn_save" button, and the name of the dialog is "Mp3TagFile", what would the code be so that when I press the button, all the changes made in the dialog are saved?

	if (msg.event == "click") { 
		switch (msg.control) {
			case "btn_guardar": Mp3TagFile(dlg);
		}
    }

It's not as quick as you might think... you need to read all the values from the object and write them to the file with SetAttr META.

I understood, I will work based on that.

Somebody help me please. How do I have to do so that, when clicking on a button with the name btn_guardar, the following command is applied?

cmd.RunCommand('SetAttr META "title:' + dlg.Control("titulo").label + '"');

	if (msg.event == "click") {
		switch (msg.control) {
			case "btn_guardar": cmd.RunCommand('SetAttr META "title:' + dlg.Control("titulo").label + '"');
		}
    }

@lxp Can you give me some other help here? :pray:

It is in the docs, topic: "Interacting with Dialog Controls"

Looks okay. If it's not working it's probably because FILE= is missing from the command.

Thanks @Xyzzy, but that example is in VBScript, different from JScript. I have searched a lot in the manual and in the forum but nothing applies at all, and on Google it is worse, a thousand different programming languages :smile:

Thanks @lxp, so the code structure (syntax) is ok? That is the part that worries me the most, the one related to SetAttr META I understand better.

I'm going to try, hopefully it works:

cmd.RunCommand('SetAttr' + ' FILE="' + item + '"' + ' META "title:' + title_name + '"'); 

I continue to have problems with the buttons, they do not respond, I asked for help outside the forum and they told me that I should reevaluate the variable in the following line switch (msg.control) {, but I can't correct that, can someone help me?

Complete code

// Mp3 Tag File
// (c) 2024 DASOTA

// Script para Directory Opus.

// Iniciar el script
function OnInit(initData)
{
	initData.name = "Mp3 Tag File";
	initData.version = "1.0";
	initData.copyright = "(c) 2024 DASOTA";
	initData.desc = "Editor Mp3 Tag de archivo único";
	initData.default_enable = true;
	initData.min_version = "13.0";
}

// Añadir comando
function OnAddCommands(addCmdData)
{
	var cmd = addCmdData.AddCommand();
	cmd.name = "Mp3TagFile";
	cmd.method = "OnMp3TagFile";
	cmd.desc = "";
	cmd.label = "Mp3 Tag File";
	cmd.template = "Mp3TagFile"; //Nombre del objeto Diálogo (pestaña Recursos)
	cmd.hide = false;
	cmd.icon = "script";
}

// Implementar el comando Mp3TagFile
function OnMp3TagFile(scriptCmdData) {
    var dlg = scriptCmdData.func.Dlg();
    dlg.title = "Mp3 Tag File - Directory Opus"; //Nombre de la barra de Título
    dlg.template = 'Mp3TagFile';
	dlg.detach = true;
	dlg.Create //Crear el diálogo
		
	//Definir la variable item
	var tab = scriptCmdData.func.sourcetab;
	var item = tab.selected_files(0); //item = Al primer archivo seleccionado (0); si fuera (1) sería el segundo

	// Asignarle los valores a los controles
	dlg.Control("archivo").value = item.name_stem; //Nombre de archivo sin extensión
	dlg.Control("titulo").value = item.metadata.audio.mp3title;
	dlg.Control("artista").value = item.metadata.audio.mp3artist;
	dlg.Control("album").value = item.metadata.audio.mp3album;
	dlg.Control("anio").value = item.metadata.audio.mp3year;
	dlg.Control("pista").value = item.metadata.audio.mp3track;
	dlg.Control("genero").value = item.metadata.audio.mp3genre;
	dlg.Control("comentario").value = item.metadata.audio.mp3comment;
	dlg.Control("artista_album").value = item.metadata.audio.mp3albumartist;
	dlg.Control("compositor").value = item.metadata.audio.composers;
	dlg.Control("numero_disco").value = item.metadata.audio.mp3disc;

	//Contenido carátula
	if (item.metadata.audio.coverart > 0) {
   		var tmpFile = DOpus.FSUtil().GetTempFile();
    	tmpFile.Write(item.metadata.audio.coverart(0).data);
    	tmpFile.Close();
    	dlg.Control("caratula").label = Script.LoadImage(tmpFile);
	} else {
    	dlg.Control("caratula").label = '';
	}

	//Fondo carátula (blanco)
	dlg.Control("caratula").bg = "#FFFFFF"; 

	//Contenido resolución	
	if (item.metadata.audio.coverart > 0) {
   		var tmpFile = DOpus.FSUtil().GetTempFile();
    	tmpFile.Write(item.metadata.audio.coverart(0).data);
    	tmpFile.Close();
		var reso = item.metadata.audio.coverart(0);
    	dlg.Control("resolucion").label = reso.width + " x " + reso.height  + " x " + reso.depth + " (" + reso.size.fmt + ")";
	} else {
    	dlg.Control("resolucion").label = '';
	}
	
	//Fondo etiqueta
	dlg.Control("resolucion").fg = "#000000"; //Texto (negro)	
	dlg.Control("resolucion").bg = "#FFFFFF"; //Fondo (blanco)

	dlg.Show //Mostrar el diálogo

	while (true) { //Mientras verdadero
		msg = dlg.GetMsg(); //Mostrar el diálogo
		if (!msg.result) break; //Cuando falso, cerrar el diálogo

	//Establecer los eventos
	if (msg.event == "click") { //Eventos del tipo clic 
		switch (msg.control) {
      
			case "btn_anadir_caratula":
	    var coverart_usuario = dlg.open("Carátula"); //Solicitar carátula
      cmd.RunCommand('SetAttr' + ' META "coverart:' + coverart_usuario + '"'); //Establecer carátula
				break;

			case "btn_eliminar_caratula":
      cmd.RunCommand('SetAttr META coverart -'); //Eliminar carátula
				break;

			case "btn_eliminar_todo":
      cmd.RunCommand('SetAttr META track title artist album genre year comment coverart composers albumartist discnumber'); //Eliminar todo
				break;

	    case "btn_guardar":
      cmd.RunCommand('SetAttr META "title:' + dlg.Control("titulo").label + '"');
      cmd.RunCommand('SetAttr META "artist:' + dlg.Control("artista").label + '"');
      cmd.RunCommand('SetAttr META "album:' + dlg.Control("album").label + '"');
      cmd.RunCommand('SetAttr META "year:' + dlg.Control("anio").label + '"');
      cmd.RunCommand('SetAttr META "track:' + dlg.Control("pista").label + '"');
      cmd.RunCommand('SetAttr META "genre:' + dlg.Control("genero").label + '"');
      cmd.RunCommand('SetAttr META "comment:' + dlg.Control("comentario").label + '"');
      cmd.RunCommand('SetAttr META "albumartist:' + dlg.Control("artista_album").label + '"');
      cmd.RunCommand('SetAttr META "composers:' + dlg.Control("compositor").label + '"');
      cmd.RunCommand('SetAttr META "disc:' + dlg.Control("numero_disco").label + '"');
      cmd.RunCommand('SetAttr META "coverart:' + dlg.Control("caratula").label + '"');
		  }
    }
  }
}

==SCRIPT RESOURCES
<resources>
	<resource name="Mp3TagFile" type="dialog">
		<dialog fontsize="9" height="180" lang="esm" title="Mp3TagFile" width="332">
			<control halign="left" height="8" name="archivo_t" title="Archivo:" type="static" valign="top" width="48" x="8" y="10" />
			<control halign="left" height="12" name="archivo" readonly="yes" type="edit" width="148" x="58" y="8" />
			<control halign="left" height="8" name="titulo_t" title="Título:" type="static" valign="top" width="48" x="8" y="26" />
			<control halign="left" height="12" name="titulo" type="edit" width="148" x="58" y="25" />
			<control halign="left" height="8" name="artista_t" title="Artista:" type="static" valign="top" width="48" x="8" y="43" />
			<control halign="left" height="12" name="artista" type="edit" width="148" x="58" y="42" />
			<control halign="left" height="8" name="album_t" title="Álbum:" type="static" valign="top" width="48" x="8" y="60" />
			<control halign="left" height="12" name="album" type="edit" width="148" x="58" y="59" />
			<control halign="left" height="8" name="anio_t" title="Año:" type="static" valign="top" width="48" x="8" y="78" />
			<control halign="center" height="12" name="anio" number="yes" type="edit" updown="yes" val_min="1" width="50" x="58" y="76" />
			<control halign="left" height="8" name="pista_t" title="Pista:" type="static" valign="top" width="18" x="136" y="78" />
			<control halign="center" height="12" name="pista" number="yes" type="edit" updown="yes" val_min="1" width="30" x="157" y="76" />
			<control halign="left" height="8" name="genero_t" title="Género:" type="static" valign="top" width="48" x="8" y="94" />
			<control edit="yes" height="40" name="genero" sort="yes" type="combo" width="148" x="58" y="93">
				<contents>
					<item text="Romántico" />
					<item text="Balada" />
					<item text="Bolero" />
					<item text="Trova" />
					<item text="Patrio" />
					<item text="Son" />
					<item text="Salsa" />
					<item text="Guaracha" />
					<item text="Montuno" />
					<item text="Cumbia" />
					<item text="Merengue" />
					<item text="Tropical" />
					<item text="Ranchera" />
					<item text="Reggae" />
					<item text="Reggaetón" />
					<item text="Hip Hop" />
					<item text="Pop" />
					<item text="Rock" />
					<item text="Tecno" />
					<item text="Electrónico" />
					<item text="Jazz" />
					<item text="Infantil" />
					<item text="Religioso" />
					<item text="Instrumental" />
					<item text="Vocal" />
					<item text="Efecto sonoro" />
					<item text="Inglés" />
					<item text="Bossa Nova" />
					<item text="Tropicália" />
					<item text="Samba" />
					<item text="Pagode" />
					<item text="Lambada" />
					<item text="MPB" />
					<item text="Forró" />
					<item text="Sertanejo" />
					<item text="Sertanejo universitário" />
					<item text="Axé" />
					<item text="Funk" />
					<item text="Gospel" />
					<item text="Brega" />
					<item text="Choro" />
					<item text="Frevo" />
					<item text="Baião" />
					<item text="Piseiro" />
					<item text="Pisadinha" />
				</contents>
			</control>
			<control halign="left" height="8" name="comentario_t" title="Comentario:" type="static" valign="top" width="48" x="8" y="111" />
			<control halign="left" height="12" name="comentario" type="edit" width="148" x="58" y="110" />
			<control halign="left" height="8" name="artista_album_t" title="Artista álbum:" type="static" valign="top" width="48" x="9" y="128" />
			<control halign="left" height="12" name="artista_album" type="edit" width="148" x="59" y="127" />
			<control halign="left" height="8" name="compositor_t" title="Compositor:" type="static" valign="top" width="48" x="8" y="145" />
			<control halign="left" height="12" name="compositor" type="edit" width="148" x="58" y="144" />
			<control halign="left" height="8" name="numero_disco_t" title="Número disco:" type="static" valign="top" width="48" x="8" y="162" />
			<control halign="left" height="12" name="numero_disco" type="edit" updown="yes" val_min="1" width="148" x="58" y="161" />
			<control halign="center" height="8" name="resolucion" type="static" valign="top" width="100" x="218" y="95" />
			<control height="122" name="caratula_t" title="Carátula" type="group" width="113" x="212" y="4" />
			<control halign="center" height="86" image="yes" name="caratula" type="static" valign="center" width="100" x="218" y="15" />
			<control height="14" name="btn_anadir_caratula" title="Añadir..." type="button" width="41" x="224" y="107" />
			<control height="14" name="btn_eliminar_caratula" title="Eliminar" type="button" width="41" x="272" y="107" />
			<control height="15" name="btn_eliminar_todo" title="Eliminar todo" type="button" width="76" x="229" y="135" />
			<control default="yes" height="15" name="btn_guardar" title="Guardar" type="button" width="76" x="229" y="155" />
		</dialog>
	</resource>
</resources>

You need to define a command object

var cmd = scriptCmdData.func.command;

and use value instead of label, e.g.

cmd.RunCommand('SetAttr META "title:' + dlg.Control("titulo").value + '"');
1 Like

Thank you very much @lxp, I'll test it!

The script is already starting to have life, weak breathing, but it is already breathing :smile:

When I run the script, all the modifications I make are saved correctly when I press the "Save" button, except those in the "Genre" field (Combobox), which sometimes saves the value "-1", other times "Jazz+Funk", etc

Regardless of whether I modify the "Gender" field or not, every time I press the "save" button, after saving the changes the following error is generated.

2024-02-02 05 46 01

I would also like it if pressing the "btn_guardar" button would close the window; and that when pressing the buttons, "btn_eliminar_caratula" and "btn_eliminar_todo", their respective values will also be deleted in the window and not only in the file.

Try to see if you can help me with those things, for my mental health, I'm going to go crazy with this script :smile:

For genre you need to use

dlg.Control("genero").value.name

Please study Control to understand why this is different from the others.


This was some good advice, too good to ignore:

1 Like

Don't think that what I don't achieve here is due to lack of study because I dedicate a lot of interest and effort to everything that you guide me or recommend, it's just that for me it is very difficult, they are two completely unknown languages, English and programming, apart from that we do not all have the same abilities.

Getting back to the topic, now the gender is set correctly, but when I open the file a second time, the gender displayed is not actually the one it has, but rather the first one in the list.

Could you also please show me how to remove the value of a text field from the window? Thank you so much.

Set the field to an empty string, e.g.:

dlg.Control("titulo").value = '';
1 Like

Now I have the problem with the "btn_anadir_cover" button, it allows me to select a new cover, but immediately afterwards generates an error and restarts Opus.

    case "btn_anadir_caratula":
var coverart_usuario = dlg.open("Carátula");
dlg.Control("caratula").label = coverart_usuario;
    break;

And if I change dlg.Control("caratula").label = coverart_usuario;
for dlg.Control("caratula").label = Script.LoadImage(tmpFile);

The image is not updated, it always shows the old one, which is normal.

Try

dlg.Control("caratula").label = Script.LoadImage(coverart_usuario);
1 Like