How to set a default string in a dialog box?


I want to set \[শিল্পী.* This string in <control halign="left" height="12" name="findWhat" tip="Find What" type="edit" width="130" x="12" y="35" />
This Dialog box default value. How can I do it?

For an Edit control, you have to create the dialog and then set the control's text in the script code.

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dlg = clickData.func.Dlg();
    cmd.deselect = false; // Prevent automatic deselection

    if (tab.selected_files.count == 0) return;

    dlg.template = 'Find and Replace Music Metadata';
    dlg.Show();
    
    if (!dlg.result) return;
    
    var keyword = dlg.Control('keyword').value.name;
    var findWhat = dlg.Control('findWhat').value;
    var replaceWith = dlg.Control('replaceWith').value;
    
    if (findWhat == '') return;
    
    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (item.metadata == 'none') continue;
        var itemKey = item.metadata.audio[keyword];
        if (typeof itemKey != 'string') continue;
        var cmdLine = 'SetAttr' +
        ' FILE="' + item + '"' +
        ' META' +
        ' "' + keyword.replace('mp3', '') + ':' + itemKey.replace(new RegExp(findWhat, 'g'), replaceWith) + '"';
        
        DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
}

This is my Script code where do I set the text?

Similar to here:

Dlg.Create();
    dlg.Control('findWhat').value = " \[শিল্পী.*";
    var findWhat = Dlg.RunDlg(); 

After Read the post I try to write this but it's does not work. Can you help me to solve the problem.

Dlg ≠ dlg?

Ok I have tried dlg instead of Dlg but steel not works

dlg.Create();
    dlg.Control('findWhat').value = " \[শিল্পী.*";
    var findWhat = dlg.Rundlg();

Can you post your resources (from the first screenshot) in text form?

dlg.Create();
    dlg.Control('findWhat').value = " \[শিল্পী.*";
    dlg.RunDlg();
    if (!dlg.result) return
    var findWhat = dlg.Control('findWhat').value;
<resources>
	<resource name="Find and Replace Music Metadata" type="dialog">
		<dialog fontsize="10" height="126" lang="english" resize="yes" standard_buttons="ok,cancel" title="Find &amp; Replace in Music Metadata" width="168">
			<control height="40" name="keyword" type="combo" width="133" x="14" y="21">
				<contents>
					<item text="mp3artist" />
					<item text="mp3album" />
					<item text="mp3albumartist" />
					<item text="mp3genre" />
					<item text="composers" />
					<item text="copyright" />
					<item text="Publisher" />
					<item text="mp3year" />
				</contents>
			</control>
			<control halign="left" height="12" name="findWhat"  tip="find what" type="edit" width="133" x="14" y="54" />
			<control halign="left" height="12" name="replaceWith" tip="Replace with" type="edit" width="133" x="14" y="90" />
			<control halign="left" height="12" name="static1" title="Find Where [Select Metadata field]" type="static" valign="top" width="133" x="14" y="7" />
			<control halign="left" height="12" name="static2" title="Find What" type="static" valign="top" width="105" x="14" y="42" />
			<control halign="left" height="12" name="static3" title="Replace with" type="static" valign="top" width="126" x="14" y="77" />
		</dialog>
	</resource>
</resources>

Ok I do as you say but it's steel not works for me.
it's steel show like that one
image
but I want the dialog box like this one
image
where the value is already here. I didn't type each time. But now I can press backspace and can delete this value and type new value if I need.
here is my full script:

// https://resource.dopus.com/t/how-to-find-and-replace-in-composers-and-artist-column-meta-data-in-selected-flac-files/41265/23

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dlg = clickData.func.Dlg();
    cmd.deselect = false; // Prevent automatic deselection

    if (tab.selected_files.count == 0) return;

    dlg.template = 'Find and Replace Music Metadata';
    dlg.Show();
    
    if (!dlg.result) return;
    
    var keyword = dlg.Control('keyword').value.name;
	dlg.Create();
    dlg.Control('findWhat').value = " \[শিল্পী.*";
	dlg.RudDlg();
	if (!dlg.result) return
    var findWhat = dlg.Control('findWhat').value; 
	var replaceWith = dlg.Control('replaceWith').value;
    
    if (findWhat == '') return;
    
    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (item.metadata == 'none') continue;
        var itemKey = item.metadata.audio[keyword];
        if (typeof itemKey != 'string') continue;
        var cmdLine = 'SetAttr' +
        ' FILE="' + item + '"' +
        ' META' +
        ' "' + keyword.replace('mp3', '') + ':' + itemKey.replace(new RegExp(findWhat, 'g'), replaceWith) + '"';
        
        DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
}

Here is the resources

<resources>
	<resource name="Find and Replace Music Metadata" type="dialog">
		<dialog fontsize="10" height="126" lang="english" resize="yes" standard_buttons="ok,cancel" title="Find &amp; Replace in Music Metadata" width="168">
			<control height="40" name="keyword" type="combo" width="133" x="14" y="21">
				<contents>
					<item text="mp3artist" />
					<item text="mp3album" />
					<item text="mp3albumartist" />
					<item text="mp3genre" />
					<item text="composers" />
					<item text="copyright" />
					<item text="Publisher" />
					<item text="mp3year" />
				</contents>
			</control>
			<control halign="left" height="12" name="findWhat"  tip="find what" type="edit" width="133" x="14" y="54" />
			<control halign="left" height="12" name="replaceWith" tip="Replace with" type="edit" width="133" x="14" y="90" />
			<control halign="left" height="12" name="static1" title="Find Where [Select Metadata field]" type="static" valign="top" width="133" x="14" y="7" />
			<control halign="left" height="12" name="static2" title="Find What" type="static" valign="top" width="105" x="14" y="42" />
			<control halign="left" height="12" name="static3" title="Replace with" type="static" valign="top" width="126" x="14" y="77" />
		</dialog>
	</resource>
</resources>

// https://resource.dopus.com/t/how-to-find-and-replace-in-composers-and-artist-column-meta-data-in-selected-flac-files/41265/23

function OnClick(clickData) {
    var cmd = clickData.func.command;
    var tab = clickData.func.sourcetab;
    var dlg = clickData.func.Dlg();
    cmd.deselect = false; // Prevent automatic deselection
    if (tab.selected_files.count == 0) return;

    dlg.template = 'Find and Replace Music Metadata';
    //?dlg.Show();
    
    //?if (!dlg.result) return;
    
	dlg.Create();
    dlg.Control('findWhat').value = " \[শিল্পী.*";
	//dlg.RudDlg();
	dlg.RunDlg();
	if (!dlg.result) return
	var keyword = dlg.Control('keyword').value.name;
    var findWhat = dlg.Control('findWhat').value; 
	var replaceWith = dlg.Control('replaceWith').value;
    
    if (findWhat == '') return;
    
    for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
        var item = e.item();
        if (item.metadata == 'none') continue;
        var itemKey = item.metadata.audio[keyword];
        if (typeof itemKey != 'string') continue;
        var cmdLine = 'SetAttr' +
        ' FILE="' + item + '"' +
        ' META' +
        ' "' + keyword.replace('mp3', '') + ':' + itemKey.replace(new RegExp(findWhat, 'g'), replaceWith) + '"';
        
        DOpus.Output(cmdLine);
        cmd.RunCommand(cmdLine);
    }
}
1 Like

dlg.Control('findWhat').value = " \[শিল্পী.*";
Hi there is a back slash \ in front of the value. But when I run the button that back slash \ is missing in find what dialog box. Actually it's a regular expression so the back slash \ is must needed. How to solve that?
image
Notice that the back slash \ is missing in find what dialog box.
But it's should be like that
image

Backslash is an escape character in JScript strings. To get a literal backslash, use \\.

1 Like

Thank you so much leo, problem solved.

1 Like