0

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()
RobC
  • 22,977
  • 20
  • 73
  • 80

3 Answers3

3

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\"" );
 }
}
Loic
  • 2,173
  • 10
  • 13
  • Could you explain what `this` refers to in `w.close(this.code)` in the `onClick`? I understand that what you do is store the code and then based on its value run the different scenarios in the `if/else` statement.Thanks very much btw, works like a charm! – Kostas Koutoupis May 23 '17 at 12:22
  • this refers to the button from where the event was fired. I use this to avoid repeating the same handler for the three buttons but with minor difference. – Loic May 24 '17 at 09:52
1

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();
}
fabianmoronzirfas
  • 4,091
  • 4
  • 24
  • 41
0

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
user1754036
  • 396
  • 1
  • 6
  • Yes but you then reduce your code ton mac users only. ExtendScript will make the code compliant for both platforms ;) – Loic May 24 '17 at 09:53