捕获用户的点击:InDesign 脚本

Capture user's click: InDesign scripting

我正在尝试编写一个脚本,向用户显示一个包含三个选项的简单对话框:"Yes"、"No" 和 "Cancel"。根据用户的输入,脚本会生成两种不同类型的 pdf(特别报告/在线 PDF)或退出进程(如果他们单击 "Cancel")。到目前为止,这是我想出的。虽然我能够捕获用户的点击,但我无法根据用户的选择获得正确的脚本 运行:

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

如果您不需要使用 scriptUI,您可以使用 InDesign 内置对话框来执行此操作,如下所示。

/* 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();
}

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

如果任何 AppleScripter 找到此页面:

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