Search code examples
javascriptmodal-dialogadobe-indesignextendscriptadobe-scriptui

Indesign javascript ERROR: MODAL DIALOG OR ALERT IS ACTIVE adobe extendscript toolkit


I try to run my javascript code by indesign but there is the error: MODAL DIALOG OR ALERT IS ACTIVE.

Who knows how to solve this problem?

 #target "InDesign"

 w = new Window ('dialog'); 

 magicButton = w.add ("button", undefined, "Buttton");
 magicButton.onClick = main

 w.show ();

 function main(){

 var myDocument = app.activeDocument
 myDocument.viewPreferences.horizontalMeasurementUnits = 
 MeasurementUnits.PIXELS;
 myDocument.viewPreferences.verticalMeasurementUnits = 
 MeasurementUnits.PIXELS;
 }

Solution

  • The error happens, because the window with the button is still open. And while that is the case, the document cannot be changed.

    To make it work, you could use the button to just close the window and then let the main function run right afterwards:

    #target "InDesign"
    
    w = new Window('dialog');
    
    magicButton = w.add ("button", undefined, "Buttton");
    magicButton.onClick = function() {
      w.close();
    }
    
    w.show();
    
    main();
    
    function main(){
    
      // ... do stuff with the document etc.
    
    }
    

    Note that more typically you would add both an OK and Cancel button to your window and then depending on which button the user presses, you would execute your function or not.

    If you need to have the window stay open (altough I don't see how this would make any sense in the example you have given), you would neet to create a window of type palette instead of dialog. In this case you will also have to create a target engine, else the window will not show up:

    #targetengine session
    
    w = new Window('palette');
    
    // ... and so on