Skip to main content
Les Harris
Inspiring
May 28, 2020
Answered

onClick callbacks for buttons don't execute

  • May 28, 2020
  • 1 reply
  • 558 views

This is my first attempt at creating a dialog box using the "resource specification" method. Basically, I want the script to stop if the user clicks Cancel or continue if the user clicks OK. My function that handles the "OK to continue" always returns 0. Here is the code; any insights would be greatly appreciated.

 

 

#target framemaker
main();
function main () {
    var vStartDir = ChooseFile("Select the folder containing the book you want to clone:", "", "", Constants.FV_ChooseOpenDir);
    var vOutputDir = ChooseFile("Select (or create) the destination folder for the new book:", "", "", Constants.FV_ChooseOpenDir);
    var okToOverwrite = fileOverwriteWarning(); // this always evaluates to 0
    alert(okToOverwrite);  
    if (okToOverwrite == 0) { // STOP execution
        return;
        }
   
    // Otherwise, continue execution (script never gets this far)
    alert("okToOverwrite = " + okToOverwrite + ". Continuing...");
    // etc.
}
function fileOverwriteWarning()
{
    var result =0;
 var resource = "dialog { properties:{ resizeable:true }, text:'Overwrite existing files?',  msg: StaticText { text:'NOTE: If you chose an existing folder as the destination for the new book, existing files in that folder with the same name as a source file will be over-written. OK to continue?',  preferredSize: [400, 80], minimumSize: [400, 80], alignment:['fill','fill'], properties:{multiline:true} }, btnGroup: Group { alignment:['center', 'bottom'] okBtn: Button { text:'OK', properties:{name:'ok'} }, cancelBtn: Button { text:'Cancel', properties:{name:'cancel'} } } }";
   
    //Define the dialog resource based on the specification
    var dlg = new Window (resource);
   
    dlg.layout.layout(true);
    dlg.show();
 
    // callbacks...THESE NEVER EXECUTE
    dlg.btnGroup.okBtn.onClick = function()
    {
        alert("OK clicked");
        result = 1;
        };
    // Callback for clicking the Close button, just want to close the palette.
    dlg.btnGroup.cancelBtn.onClick = function()
    {
        alert("Cancel clicked");
        this.parent.parent.close(2)
        dlg = null;
 };
    //This gets called by close() above and when
    //you click X in the upper right corner.
    dlg.onClose = function()
    {
        alert("Red X clicked");
        dlg = null;
 };
    dlg.onResize = function()
    {
        dlg.layout.resize();
 }
return result;
 }

 

 

    This topic has been closed for replies.
    Correct answer Les Harris

    After thrashing away for a few more hours, I gave up on the resource specification approach, and also included the "fileOverwriteWarning" code in the main function. It may not be elegant, but it does do what I want now. Here's the new version.

    #target framemaker
    
    main();
    
    function main () {
        var vStartDir = ChooseFile("Select the folder containing the book you want to clone:", "", "", Constants.FV_ChooseOpenDir);
        var vOutputDir = ChooseFile("Select (or create) the destination folder for the new book:", "", "", Constants.FV_ChooseOpenDir);     
        // Confirm that the user wants to over-write identically-named files if vOutputDir = vStartDir
        var okToOverwrite = false;
    	var win = new Window("dialog", "Overwrite existing files?");  
        win.size = [300,160];
        win.msg = win.add("statictext", undefined, "NOTE: If you chose an existing folder as the destination for the new book, existing files in that folder with the same name as a source file will be over-written. OK to continue?",{multiline:true}); 
    	// Add Yes/No buttons
    	win.okBtn = win.add("button", [15,65,105,85], "Yes");
    	win.cancelBtn = win.add("button", [120, 65, 210, 85], "No");
    	// Register event listeners that define the button behavior
    	win.okBtn.onClick = function() {
            okToOverwrite = true;
            $.writeln("okToOverwrite = " , okToOverwrite);
    		win.close();
    	};
    	win.cancelBtn.onClick = function() {
            $.writeln("okToOverwrite = " , okToOverwrite);
    		win.close();
    	};
        win.onClose = function() 
        {
            $.writeln("okToOverwrite = " , okToOverwrite);
            win = null;
    	};
    
    	// Display the window
    	win.show();
        
        if (!okToOverwrite) { // STOP execution
            return; 
            }
    
        // Otherwise, continue execution 
        alert("okToOverwrite = " + okToOverwrite + ". Continuing...");
        // etc. etc. etc.
    
    } // end main

    1 reply

    Les Harris
    Les HarrisAuthorCorrect answer
    Inspiring
    May 29, 2020

    After thrashing away for a few more hours, I gave up on the resource specification approach, and also included the "fileOverwriteWarning" code in the main function. It may not be elegant, but it does do what I want now. Here's the new version.

    #target framemaker
    
    main();
    
    function main () {
        var vStartDir = ChooseFile("Select the folder containing the book you want to clone:", "", "", Constants.FV_ChooseOpenDir);
        var vOutputDir = ChooseFile("Select (or create) the destination folder for the new book:", "", "", Constants.FV_ChooseOpenDir);     
        // Confirm that the user wants to over-write identically-named files if vOutputDir = vStartDir
        var okToOverwrite = false;
    	var win = new Window("dialog", "Overwrite existing files?");  
        win.size = [300,160];
        win.msg = win.add("statictext", undefined, "NOTE: If you chose an existing folder as the destination for the new book, existing files in that folder with the same name as a source file will be over-written. OK to continue?",{multiline:true}); 
    	// Add Yes/No buttons
    	win.okBtn = win.add("button", [15,65,105,85], "Yes");
    	win.cancelBtn = win.add("button", [120, 65, 210, 85], "No");
    	// Register event listeners that define the button behavior
    	win.okBtn.onClick = function() {
            okToOverwrite = true;
            $.writeln("okToOverwrite = " , okToOverwrite);
    		win.close();
    	};
    	win.cancelBtn.onClick = function() {
            $.writeln("okToOverwrite = " , okToOverwrite);
    		win.close();
    	};
        win.onClose = function() 
        {
            $.writeln("okToOverwrite = " , okToOverwrite);
            win = null;
    	};
    
    	// Display the window
    	win.show();
        
        if (!okToOverwrite) { // STOP execution
            return; 
            }
    
        // Otherwise, continue execution 
        alert("okToOverwrite = " + okToOverwrite + ". Continuing...");
        // etc. etc. etc.
    
    } // end main