Adobe InDesign 2023 js - Modal dialog or alert is active

220 views Asked by At

I'm trying to turn off ligatures and hyphenations in all style sheets. The script works, but I get an alert that I have to click through.

The specific line causing the issue seems to be

!ev.isValid && app.eventListeners.add("afterOpen",myfunc).name = "onAfterOpen";

Currently, I just click no to the dialog and don't show this again, but it would be nice to remove the problem if possible. Can anyone help?

main();
function main() {
var ev = app.eventListeners.itemByName ( "onAfterOpen" );
!ev.isValid && app.eventListeners.add("afterOpen",myfunc).name = "onAfterOpen";
}

function myfunc (myEvent){
var doc = app.activeDocument;

var allCharacterStyles = doc.allCharacterStyles;
var allParagraphStyles = doc.allParagraphStyles;

//alert ("Cleaning Character Styles.");
for (i = 1; i < allCharacterStyles.length; i++) {
allCharacterStyles[i].ligatures = false;
}

//alert ("Cleaning Paragraph Styles.");
for (i = 1; i < allParagraphStyles.length; i++) {
allParagraphStyles[i].ligatures = false;
allParagraphStyles[i].hyphenation = false;
}
//alert ("All Done.");

}

I've isolated the issue to one line:

!ev.isValid && app.eventListeners.add("afterOpen",myfunc).name = "onAfterOpen";

The error suggests a window is open, but I am not triggering a window.

screenshot of error

1

There are 1 answers

6
Yuri Khristich On

Try this variant of the code:

//@target "indesign"
//@targetengine "session"

main();

function main() {
    remove_listeners('afterOpen');
    app.eventListeners.add('afterOpen', myfunc);
}

function myfunc(event) {
    if (event.parent.constructor.name !== 'LayoutWindow') return; // <-- HERE
    var doc = app.activeDocument; // <-- HERE

    // or...
    // if (event.parent.constructor.name !== 'Document') return;
    // var doc = event.parent;

    var allCharacterStyles = doc.allCharacterStyles;
    var allParagraphStyles = doc.allParagraphStyles;

    // alert ("Cleaning Character Styles.");
    for (i = 1; i < allCharacterStyles.length; i++) {
        allCharacterStyles[i].ligatures = false;
    }

    // alert ("Cleaning Paragraph Styles.");
    for (i = 1; i < allParagraphStyles.length; i++) {
        allParagraphStyles[i].ligatures = false;
        allParagraphStyles[i].hyphenation = false;
    }
    // alert ("All Done.");
}

function remove_listeners(eventType) {
    var listeners = app.eventListeners;
    var i = listeners.length;
    while (i--) if (listeners[i].eventType == eventType) listeners[i].remove();
}

Alternatively you can replace the lines:

if (event.parent.constructor.name !== 'LayoutWindow') return;
var doc = app.activeDocument;

with:

if (event.parent.constructor.name !== 'Document') return;
var doc = event.parent;

or:

if (!(event.parent instanceof Document)) return;
var doc = event.parent;

It will run the function before the main window is show.

The handler 'afterOpen' runs twice. First its event is 'Document' (in this case there is no app.activeDocument yet, you have to use var doc = event.parent to address the opened document), the second event is 'LayoutWindow'.