0

Working on a script for InDesign I found this problem : if I call app.open() from inside button.onclick() it stops and nothing happens. Since I'm a beginner with Javascript I'm probably doing something wrong. But if not, how do I fix? I can not find an alternative.

Please, only pure Javascript.

Thanks in advance.

Here the working code :

var book_info;

if (app.books.length != 1) {
    var theFile = File.openDialog ("Select the book file to open...");
    get_data(theFile);
        alert(book_info.filePath + "\r" + book_info.name);
}

book_info.close();

    function get_data(data) {
        app.open(data);
        book_info = app.activeBook;
            alert("INSIDE FUNCTION" + book_info.filePath + "\r" + book_info.name);
        return data;
    }

and here the one not working :

var book_info;

var w1 = new Window ("dialog", "TEST");
    w1.minimumSize.height = 50;
    w1.minimumSize.width = 50;

var p1 = w1.add ("panel");
        sel_button = p1.add ("button", undefined, "Select book");

var g1 = w1.add ("group");
        g1.add("button", undefined, "Cancel");
        g1.add("button", undefined, "OK");

sel_button.onClick = function(){
    var theFile = File.openDialog ("Select the book file to open...");
    get_data(theFile);
        alert(book_info.filePath + "\r" + book_info.name);
    book_info.close();
};

w1.show();


function get_data(data) {
    app.open(data);
    book_info = app.activeBook;
        alert("INSIDE FUNCTION" + book_info.filePath + "\r" + book_info.name);
    return data;
}
  • It does not do anything, it returns an error - "Cannot handle the request because a modal dialogue is active". You can use w1.close();, but you will have the same error. When you call a function attached to a window it will assume there is a window (even if you close it ) and stop some of the functions. I had a similar problem before and have to change script completely completely – Nicolai Kant Mar 23 '15 at 18:13
  • @nicolai.kant To me it returns no errors at all, it just stops. Could you please give me an example on how I have to change my script? At the moment I have no idea.... Thanks. – Alessio Tonarelli Mar 23 '15 at 18:21

1 Answers1

0

There is an alternative answer at app.open not responding if called from inside onclick()

It also suggested the use of 'palette' window instead of modal dialogue

Try this:

var book_info;     
var theFile;   
var getData;  
var w1 = new Window ("dialog", "TEST");     
    w1.minimumSize.height = 50;     
    w1.minimumSize.width = 50;     
    var p1 = w1.add ("panel");     
        sel_button = p1.add ("button", undefined, "Open a book");     
    var g1 = w1.add ("group");     
        g1.add("button", undefined, "Cancel");     
        g1.add("button", undefined, "OK");     

sel_button.onClick = function(){     
    theFile = File.openDialog ("Select the book file to open...");    
  getData =1;           
  w1.close(1);   
};     

w1.show()  

if (  getData ==1) {   
  if ( theFile ) {   
  get_data(theFile);     
  }   
}   

function get_data(data) {     
    app.open(data);     
    book_info = app.activeBook;     
        alert("INSIDE FUNCTION" + book_info.filePath + "\r" + book_info.name);     
    return data;     
}  
Nicolai Kant
  • 1,391
  • 1
  • 9
  • 23
  • In my case the best solution is to change from "dialog" to "palette". This allows me to not make changes to the code already written. Thanks for your help. – Alessio Tonarelli Mar 24 '15 at 21:06