Javascript drop down menu selection calls a function

Contributor ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

Hi all,

I'm in over my head once again. Currently trying to learn JavaScript and trying to run before I can walk. Hey ho.

How do I go about calling one of three different functions based on the user's selection from a drop-down menu?

I can only seem to get it to do A4. And it also runs the function when I click cancel (which it shouldn't do).

Any help, as always, is very much appreciated.

 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
if(myDialog.show() == true){
  alert (myList [myDropSelection.selectedIndex])
  myDialog.destroy();
  }

if([selectedIndex = 0]){
        exportA4();
}

if([selectedIndex = 1]){
        exportUS();
}

if([selectedIndex = 2]){
        exportBoth();
}
 

function exportA4() {//code here}

function exportUS() {//code here}

function exportBoth() {//code here}

 

TOPICS
Import and export , Scripting

Views

105

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Guru , Jun 07, 2022 Jun 07, 2022

It was almost 20 years ago the last time I used the dialogs object. 🙂 I guess it should be something like that:

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
if(myDialog.show
...

Likes

Translate

Translate
Adobe Community Professional , Jun 07, 2022 Jun 07, 2022

A few errors with the code. When comparing things, use the == comparator, not =. Also, no need to have those items in brackets. Good to store the selection during show then check that selection after destroying. 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var 
...

Likes

Translate

Translate
Guru ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

It was almost 20 years ago the last time I used the dialogs object. 🙂 I guess it should be something like that:

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
if(myDialog.show() == true){
	
	if(myDropSelection.selectedIndex == 0){
			exportA4();
	}
	else if(myDropSelection.selectedIndex == 1){
		exportUS();
	}
	else if(myDropSelection.selectedIndex == 2){
		exportBoth();
	}

	myDialog.destroy();
}

function exportA4() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

function exportUS() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

function exportBoth() {
	$.writeln(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

Hi @Kasyan Servetsky, just a little thing I noticed in passing... functions have a name property so you should be able to simply to arguments.callee.name. Tiny thing but I like it when people tell me these things so hope you don't mind either.

- Mark

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Jun 07, 2022 Jun 07, 2022

Copy link to clipboard

Copied

A few errors with the code. When comparing things, use the == comparator, not =. Also, no need to have those items in brackets. Good to store the selection during show then check that selection after destroying. 

var myList = ["A4", "US Letter", "A4 & US Letter"];
var myDialog = app.dialogs.add({name:"PDF Export", canCancel:true});
with (myDialog )
    with (dialogColumns.add())
        with(borderPanels.add())
            {
            staticTexts.add ({staticLabel:"Select:"});
            var myDropSelection = dropdowns.add({stringList:myList, selectedIndex:2});
            }
var mySel;
if(myDialog.show()){
    mySel = myDropSelection.selectedIndex;
    myDialog.destroy();
  }

if(mySel == 0){
        exportA4();
}

if(mySel == 1){
        exportUS();
}

if(mySel == 2){
        exportBoth();
}
 

function exportA4() { alert("Export A"); }

function exportUS() { alert("Export US"); }

function exportBoth() { alert("Export Both"); }

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

Thanks, guys. Both of your suggestions worked perfectly.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 08, 2022 Jun 08, 2022

Copy link to clipboard

Copied

LATEST
quote

Thanks, guys. Both of your suggestions worked perfectly.


By @JustyR

Great minds think alike! 🙂

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines