Capture user's click: InDesign scripting

1.6k Views Asked by At

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()
3

There are 3 best solutions below

2
On BEST ANSWER

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\"" );
 }
}

0
On

If you don't need to use scriptUI you can do this using the InDesign build in dialogs like this.

/* global app, $*/
var diag = app.dialogs.add({
  name: 'my awesome dialog',
  canCancel: true
});
var col1 = diag.dialogColumns.add();
var radioGrp = col1.radiobuttonGroups.add();
var ctrl1 = radioGrp.radiobuttonControls.add({
  staticLabel: 'selection1',
  checkedState: true
});
var ctrl2 = radioGrp.radiobuttonControls.add({
  staticLabel: 'selection2'
});

if(diag.show() === true) {
  if(ctrl1.checkedState === true) {
    $.writeln('user selected item 1');
  } else if(ctrl2.checkedState === true) {
    $.writeln('user selected item 2');

  }
  diag.destroy();
}
1
On

In case any AppleScripters find this page:

if button returned of ¬
    (display dialog "Is this a Special Report?" buttons {"Yes", "No", "Cancel"} default button 2) = "Yes" then
    my exportSpecialReport()
else
    my exportOnlinePDF()
end if