Set up dialog box in jscript?

Hello everybody

I have created a button with this code.

SetAttr META "Copyright:{dlgstring| write the name for copyright \nThen click OK.}"

now I want to create a button for do the same job with in jscript.
but I don't know how to create or set a dialog box in jscript.
I have search the documentation then

https://www.gpsoft.com.au/help/opus12/index.html#!Documents/Scripting/simpledialogsandpopupmenus.htm
I have found this documentation. here all example in vbscript. Is there any documentation like the same example in jscript?

For the things on that page, there's almost no difference between the languages, other than needing ; at the end of statements and () fo function calls with no arguments.

Detached Dialogs are more complex, but the manual has examples for those in both languages.

(We'll do the same for Simple Dialogs in an update, since that was already written but not published yet.)

You can find more JScript examples for Detached Dialogs on the forum. Simple one:

More advanced one:

For other types of dialogs/UI or other scripting examples, you can usually find examples by searching.

Here is the easiest way

function OnClick(clickData)
{
    var name = DOpus.dlg.GetString("Write the name for copyright");
    DOpus.Output(name);
}

screenshot

If user clicks Cancel you'll get an empty string.

If you want a modal input box you can replace DOpus.dlg part with clickData.Func.sourceTab.dlg (then the input box will be topmost and will require interaction).

Ok! I have customize this Dialog box which is get from the forum.
image
Here is my current script code

function OnClick(clickData) {
    var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "MusicTag";
    Dlg.detach = true;
    Dlg.Show();
    while (true) {
        var Msg = Dlg.GetMsg();
     
    }
    DOpus.Output("Return code = " + Dlg.result);
}

Here is my resources

<resources>
        <resource name="MusicTag" type="dialog">
       <dialog fontsize="8" height="201" lang="english" resize="yes"
           standard_buttons="ok,cancel" title="Personal Information" width="153">


	   <control halign="left" height="8" name="static1" title="©:" type="static"
              width="10" x="29" y="7" />
           <control halign="left" height="12" name="Copyright" tip="Enter Copyright Company name"
              type="edit" width="106" x="39" y="6" />

  
          <control halign="left" height="8" name="static4" title="Genre:"
              type="static" width="27" x="5" y="76" />
           <control height="40" name="Genre" type="combo" width="104" x="39" y="75">
              <contents>
                  <item text="Soundtek" />
                  <item text="G Series" />
                  <item text="সংগীতা" />
                  <item text="সোনালী প্রোডাক্টস্‌" />
                  <item text="Anupam Recording Media" />
                  <item text="Laser vision Ltd." />
                  <item text="CD Vision" />
                  <item text="CD Choice" />
              </contents>
           </control>


	  </dialog>
    </resource>
</resources>

Now, how can I get the selected Item from the combo list item and the prompt box into variable? then I can use the variables data for update the selected files copyright column.

I think that in this particular example you don't need to have a detached dialog loop, so here is an example without detached dialog loop (this means that after the Dlg.Show(); the execution is "blocking" until the dialog is dismissed):

function OnClick(clickData) {
    var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "MusicTag";
    //Dlg.detach = true; // don't need this anymore
    Dlg.Show();

	if (Dlg.result) {
		var name = Dlg.control("Copyright").value
		var genre = Dlg.control("Genre").value.name
		DOpus.Output("Copyright: " + name);
		DOpus.Output("Genre: " + genre);	
	}
	else {
		DOpus.Output("User cancelled");	
	}

    DOpus.Output("Return code = " + Dlg.result);
}
1 Like

image

Hmmm works fine for me on Opus 12.23.4

Maybe something got pasted incorrectly? I've attached a button itself:
New Button.dcf (4.0 KB)

1 Like

Were you in customize mode?

1 Like

NO, the buttons returns the copyright boxes text & selected genre fine, Now I have to write something code for use this return for the column Copyright and for the Genre. If possible plz write this for me
image

OK! now my button works fine for update the genre and the copyright column. But If user Leave the dialog box popup or the combo list blank, then Press OK, then the button has remove all information from the genre and the copyright column for the selected files. But I need to do nothing if the user leave dialog box popup or the combo list blank. How can I do that?
Here is my button code:

function OnClick(clickData) {
    var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "MusicTag";
    Dlg.Show();

	if (Dlg.result) {
		var Copyright = Dlg.control("Copyright").value
		var Genre = Dlg.control("Genre").value.name

for (var enumSelected = new Enumerator(clickData.func.sourcetab.selected_files);!enumSelected.atEnd();enumSelected.moveNext()) {
		clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "Copyright:'+Copyright+'"');
		clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "genre:'+Genre+'"');

	}
//	else {
//		DOpus.Output("User cancelled");	
	}
}

Here is my button Resources:

<resources>
        <resource name="MusicTag" type="dialog">
       <dialog fontsize="8" height="201" lang="english" resize="yes"
           standard_buttons="ok,cancel" title="Personal Information" width="153">


	   <control halign="left" height="8" name="static1" title="©:" type="static"
              width="10" x="29" y="7" />
           <control halign="left" height="12" name="Copyright" tip="Enter Copyright Company name"
              type="edit" width="106" x="39" y="6" />

  
          <control halign="left" height="8" name="static4" title="Genre:"
              type="static" width="27" x="5" y="76" />
           <control height="40" name="Genre" type="combo" width="104" x="39" y="75">
              <contents>
                  <item text="Soundtek" />
                  <item text="G Series" />
                  <item text="সংগীতা" />
                  <item text="সোনালী প্রোডাক্টস্‌" />
                  <item text="Anupam Recording Media" />
                  <item text="Laser vision Ltd." />
                  <item text="CD Vision" />
                  <item text="CD Choice" />
              </contents>
           </control>


	  </dialog>
    </resource>
</resources>

Presumably the Copyright and Genre strings/variables are empty in that case.

So just make your code check if they are empty.

If you don't know how to do that, Google for "javascript test if a string is empty", or do some JavaScript tutorials, as that's very basic stuff.

Hi Leo, I had saw some tutorial about "javascript test if a string is empty", they add a If condition for check the empty box like this

if (copyright=== "")

But I don't understand now how to tell Dopus to do nothing if the dialog box is empty?
another tutorial I had saw another If condition for check white space (if user press only space with no character or press only 2 character) then first he replace all the blank space with empty then add a condition If length of the character is <=2
the code is like that:

if (copyright.replace(/\s/g), "").lenght <=2 {

}

here\s for the space and /g for global replace with "" empty. But after that how to tell Dopus to do nothing?
Can I use both of (empty and blank space) condition in one If statement or should I write 2 deferent if statement?

These are basic, fundamental programming concepts which don't relate to Opus in any way and which you can learn all over the web.

Teaching how if-statements and branching work is outside the scope of this forum, but should be well covered in every beginner programming tutorial on the web, as long as you go through them properly and don't just try to copy & paste what you need without taking in what things actually mean.

1 Like

Great help

We've given you so much help and time over the years but there is a limit, and we aren't able to write scores of custom scripts for you if you're unwilling or unable to learn yourself.

1 Like

I am very grateful for that.

You didn't write this script for me, you give me some link that was helped me, But This script Custom by me. He is bytespiller who write some code. Look man I am not a Programmer, and that is not my goal to become a programmer also. I know that very well. I have tried to create some buttons by the Help from the forum, the name of the forum is Help & Support by the way.

The forum is here to provide help & support with Opus, not help & support with everything related to computers. We don't have the resources to teach you how to program. There are lots of websites out there already that can do that, all you have to do is read them.

2 Likes

remainder for new person: I have create a dialog box for update Copyright and Genre for a Music file. The Problem was If user run the dialog box and leave it blank then press ok, The Existing Copyright and Genre Data of the file was Disappeared. So I Need some code who check If the Dialog box was blank, then the button should not do anything I mean the Existing Data should be as it is.

Ok, Now I have find a solution but I don't know is that the best way!
in this code I have add a Length Condition. If the Data length is >=2 Then the button will be work otherwise that does not do anything.
Here is my code:

function OnClick(clickData) {
    var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "MusicTag";
    Dlg.Show();


	if (Dlg.result) {
		var Copyright = Dlg.control("Copyright").value
		var Genre = Dlg.control("Genre").value.name

var stringLengthCopyright = Copyright.length;
var stringLengthGener = Genre.length;

		
		if (stringLengthCopyright >=2) {

for (var enumSelected = new Enumerator(clickData.func.sourcetab.selected_files);!enumSelected.atEnd();enumSelected.moveNext()) {
		clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "Copyright:'+Copyright+'"');
		//clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "genre:'+Genre+'"');

	}
		}


		if (stringLengthGener >=2) {

for (var enumSelected = new Enumerator(clickData.func.sourcetab.selected_files);!enumSelected.atEnd();enumSelected.moveNext()) {
		//clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "Copyright:'+Copyright+'"');
		clickData.func.command.RunCommand('SetAttr "'+enumSelected.item()+'" META "genre:'+Genre+'"');

	}
		}

      
//	else {
//		return false;	
	}
}

Is there any other best way for do so?

This is an OK solution, but since you're asking for a better way, I have an idea to guard against the unlimited amount of empty spaces (and newlines) by trimming the leading and trailing spaces (and newlines) away, here is an example of how it would work:

Input: <nothing>
Result: nothing happens

Input: <space> or
Input: <space><space><space><space><space><space><space>
Result: nothing happens

Input: <space>HELLO<space><space>
Result: you get "HELLO" without spaces

Input: <space>HELLO THERE<space>
Result: you get "HELLO THERE" (as you can see, only leading and trailing spaces/newlines are stripped, the ones that are part of the "sentence" aren't touched).

Input: <space>HELLO<newline>LINE2<space><space>
Result: you get "HELLOLINE2" without spaces and without newlines


Code example for this (see Trim string in JavaScript? - Stack Overflow for more info on trim functions).

function OnClick(clickData) {
	var Dlg = DOpus.Dlg;
    Dlg.window = clickData.func.sourcetab;
    Dlg.template = "MusicTag";
    Dlg.Show();

	if (Dlg.result) {
		var Copyright = Dlg.control("Copyright").value.fulltrim();
		if (Copyright.length > 0) {
			DOpus.Output(Copyright);
			// do whatever you need
		}
	}
}

String.prototype.fulltrim=function(){return this.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,'').replace(/\s+/g,' ');};

This String.prototype.fulltrim "magic" is how we expand the string functionality in jscript so that all strings now have a "fulltrim" method available.

In this simple way you achieve both a check against empty (or spaces-only) fields, and filtering-out any extra spaces/newlines.

1 Like