javascriptadobeadobe-indesignextendscriptadobe-scriptui

Capture user's click: InDesign scripting


I'm trying to come up with a script that presents the user with a simple dialog box with three options: "Yes", "No" and "Cancel". Based on the user's input the script then produces two different kinds of pdfs (Special Report / Online PDF) or exits the process (in case they clicked "Cancel"). This is what I've come up with so far. Although I'm able to capture the user's click, I can't get the right script to run based on the user's choice:

function checkReportType(){
    //draw window
    var askReportWindow = new Window("dialog", "Report Type");    
    askReportWindow.textmissing = askReportWindow.add('statictext{text:"Is this a Special Report?", justify:"center"}');
    var myInputGroup = askReportWindow.add("group");        
    var myButtonGroup = askReportWindow.add("group");
    myButtonGroup.alignment = "right";             
    //add buttons
    var yesButton = myButtonGroup.add("button", undefined, "Yes");   
    var noButton= myButtonGroup.add("button", undefined, "No");
    var cancelButton = myButtonGroup.add("button", undefined, "Cancel"); 
    //setting values to false for "yes" and "no" buttons
    var yesClicked = false;
    var noClicked = false;
    //change button value on click
    yesButton.onClick = function(){
        yesClicked = true;  
        //alert("k");   
    }       
    noButton.onClick = function(){
        noClicked = true; 
        //alert("l");      
    }
    //show window
    askReportWindow.show();
    //check for click
    if(yesClicked===true){
       exportSpecialReport();
    }else if(noClicked===true){
        exportOnlinePDF();
    }else{
        exit();
        askReportWindow.destroy();
    }
}//end checkReportType()

Solution

  • function main() {
    	var w = new Window ( "dialog", "Please choose to do something…" ),
    	u,
    	noBtn = w.add('button', u, 'NO' ),
    	outBtn = w.add('button', u, 'CANCEL' ),
    	yesBtn = w.add('button', u, 'YES' );
    	
    	noBtn.code = 0;
    	outBtn.code = 1;
    	yesBtn.code = 2;
    	
    	w.preferredSize.width = 150;
    	w.alignChildren = ["fill", "top"];
    	
    	noBtn.onClick = outBtn.onClick = yesBtn.onClick = function() {
    		w.close(this.code)
    	}
    
    	var result = w.show();
    
    	if ( result == 0 ) {
    		alert( "user clicked \"NO\"" );
    	}
    	else if ( result == 1 ) {
    		alert( "user clicked \"CANCEL\"" );
    	}
    	else {
    		alert( "user clicked \"YES\"" );
    	}
    }