javascriptwindowadobe-indesignextendscriptpalette

InDesign - making an independent window that will stay open while making corrections in the document


var Widows_Report = new Window("dialog", "Let's Find Some Widows", undefined, {borderless: false, resizeable: false});
Widows_Report.orientation = "column";
Widows_Report.alignChildren = ["center", "top"];
Widows_Report.spacing = 10;
Widows_Report.margins = 16;

var panel1 = Widows_Report.add("panel", undefined, "Select the pages you want to search");
panel1.orientation = "column";
panel1.alignChildren = ["center", "center"];
panel1.spacing = 10;
panel1.margins = [10, 10, 9, 10];

var Page_Input = panel1.add("group");
Page_Input.orientation = "row";
Page_Input.alignChildren = ["center", "center"];
Page_Input.spacing = 10;
Page_Input.margins = 6;

var edittext1 = Page_Input.add('edittext', undefined, undefined, {name: "edittext1"});
edittext1.helpTip = "Enter the page number where you want the search to start";
edittext1.preferredSize.width = 45;
edittext1.preferredSize.height = 23;

var statictext1 = Page_Input.add("statictext", undefined, "Thru");
statictext1.preferredSize.width = 52;
statictext1.preferredSize.height = 17;
statictext1.justify = "center";

var edittext2 = Page_Input.add('edittext', undefined, undefined, {name: "edittext2"});
edittext2.helpTip = "Enter the page number where you want the search to end";
edittext2.preferredSize.width = 45;
edittext2.preferredSize.height = 23;

var group2 = Widows_Report.add("group");
group2.orientation = "column";
group2.alignChildren = ["left", "center"];
group2.spacing = 10;
group2.margins = 1;

var group3 = group2.add("group");
group3.orientation = "row";
group3.alignChildren = ["center", "top"];
group3.spacing = 10;
group3.margins = 0;

var button1 = group3.add("button", undefined, "Start");
button1.preferredSize.width = 68;

var button2 = group3.add("button", undefined, "Close");
button2.preferredSize.width = 61;

var group4 = group2.add("group");
group4.orientation = "column";
group4.alignChildren = ["center", "bottom"];
group4.spacing = 12;
group4.margins = 5;

// Start Button Click Event
button1.onClick = function() {
    var startPage = parseInt(edittext1.text);
    var endPage = parseInt(edittext2.text);

    // Validate page numbers
    if (isNaN(startPage) || isNaN(endPage) || startPage < 1 || startPage > app.activeDocument.pages.length || endPage < 1 || endPage > app.activeDocument.pages.length || startPage > endPage) {
        alert("Invalid page numbers. Please enter valid page numbers.");
    } else {
        var reportWidow = "HERE ARE YOUR RESULTS:\nThese pages have widows\n\n_______________________\n\n";         
                                              
        for (var i = startPage - 1; i < endPage; i++) {
            var page = app.activeDocument.pages[i];
            var textFrames = page.textFrames;
            for (var j = 0; j < textFrames.length; j++) {
                var textFrame = textFrames[j];
                var paragraphs = textFrame.paragraphs;
                for (var k = 0; k < paragraphs.length; k++) {
                    var paragraph = paragraphs[k];
                    var fontSize = paragraph.pointSize;
                    if (fontSize >= 9.5 && fontSize <= 11) {
                        var lastLine = paragraph.lines[-1];
                        if (lastLine && lastLine.words.length === 1) {
                            reportWidow += "PAGE " + (i + 1) + ",     PARAGRAPH " + (k + 1) + "\r";
                                reportWidow += lastLine.words[0].contents + "\r\r";
                            }
                        }
                    }
                }
            }

        if (reportWidow !== "HERE ARE YOUR RESULTS:\n") {
            alert(reportWidow);
        } else {
           alert("Congratulations, No Widows Were Found!");
        }
    }
};

// Close Button Click Event
button2.onClick = function() {
    Widows_Report.close();
};

Widows_Report.show();

I need the script to run, find any widows (paragraphs of text with only one word on the last line.) and then produce the search results inside of the original script window (instead of a second alert window which it does now). The search results are correct but I need to be able to look at the results (list of widows) while making corrections in the inDesign document.

2 main issues:

1 - Script Window Format - I can't figure out how to make the script window independent so that I can still work in inDesign while looking at the search results. Sometimes the results are a long list and I need to have the results shown until I close the window.

2 - Script Search Results - Right now, the script will run and properly find the widows, the problem is, the script produces a second alert window with the search results inside. The info that is produced is correct but I need it to show in the original script window (preferred) or instead of an alert window, show the search results in a new independent window that will stay open while making corrections.

Here is a shot of the search results as they appear now:

Current Search Results Alert Window

I have scoured this site, the web and even asked chat GPT for help. I admit that I am new to inDesign scripting so these 2 issues are probably basic stuff to most people.


Solution

  • Here is the quick simply solution. It saves the final message as a text file and opens it (supposedly in Notepad on Windows or TextEdit app on macOS) instead of the alert window.

    Change the line:

    alert(reportWidow);
    

    with:

    report(reportWidow);
    

    And add at the end of the code the function:

    function report(text) {
        var report_file = File(Folder.temp + '/widows.txt');
        report_file.encoding = 'utf-8';
        report_file.open('w');
        report_file.write(text);
        report_file.execute();
        report_file.close();
    }
    

    Another approach is to turn the dialog window into a palette and add to it the text area. Here is the changed code:

    #target "InDesign"
    #targetengine "session"
    
    // "palette", not "dialog"
    var Widows_Report = new Window("palette", "Let's Find Some Widows", undefined, {borderless: false, resizeable: false});
    Widows_Report.orientation = "column";
    Widows_Report.alignChildren = ["center", "top"];
    Widows_Report.spacing = 10;
    Widows_Report.margins = 16;
    
    var panel1 = Widows_Report.add("panel", undefined, "Select the pages you want to search");
    panel1.orientation = "column";
    panel1.alignChildren = ["center", "center"];
    panel1.spacing = 10;
    panel1.margins = [10, 10, 9, 10];
    
    var Page_Input = panel1.add("group");
    Page_Input.orientation = "row";
    Page_Input.alignChildren = ["center", "center"];
    Page_Input.spacing = 10;
    Page_Input.margins = 6;
    
    var edittext1 = Page_Input.add('edittext', undefined, undefined, {name: "edittext1"});
    edittext1.helpTip = "Enter the page number where you want the search to start";
    edittext1.preferredSize.width = 45;
    edittext1.preferredSize.height = 23;
    
    var statictext1 = Page_Input.add("statictext", undefined, "Thru");
    statictext1.preferredSize.width = 52;
    statictext1.preferredSize.height = 17;
    statictext1.justify = "center";
    
    var edittext2 = Page_Input.add('edittext', undefined, undefined, {name: "edittext2"});
    edittext2.helpTip = "Enter the page number where you want the search to end";
    edittext2.preferredSize.width = 45;
    edittext2.preferredSize.height = 23;
    
    var group2 = Widows_Report.add("group");
    group2.orientation = "column";
    group2.alignChildren = ["left", "center"];
    group2.spacing = 10;
    group2.margins = 1;
    
    var group3 = group2.add("group");
    group3.orientation = "row";
    group3.alignChildren = ["center", "top"];
    group3.spacing = 10;
    group3.margins = 0;
    
    var button1 = group3.add("button", undefined, "Start");
    button1.preferredSize.width = 68;
    
    var button2 = group3.add("button", undefined, "Close");
    button2.preferredSize.width = 61;
    
    var group4 = group2.add("group");
    group4.orientation = "column";
    group4.alignChildren = ["center", "bottom"];
    group4.spacing = 12;
    group4.margins = 5;
    
    // the text area for the report
    var group5 = group2.add("group");
    group5.orientation = "column";
    group5.alignChildren = ["center", "bottom"];
    group5.spacing = 1;
    group5.margins = 1;
    
    var text_area_w = 300;
    var text_area_h = 600;
    var text_area = group5.add("edittext", [10, 16, text_area_w - 16, text_area_h - 20], " ", {multiline: true, wantReturn: true});
    
    // Start Button Click Event
    button1.onClick = function() {
        var startPage = parseInt(edittext1.text);
        var endPage = parseInt(edittext2.text);
    
        // Validate page numbers
        if (isNaN(startPage) || isNaN(endPage) || startPage < 1 || startPage > app.activeDocument.pages.length || endPage < 1 || endPage > app.activeDocument.pages.length || startPage > endPage) {
            alert("Invalid page numbers. Please enter valid page numbers.");
        } else {
            var reportWidow = "HERE ARE YOUR RESULTS:\nThese pages have widows\n\n_______________________\n\n";
    
            for (var i = startPage - 1; i < endPage; i++) {
                var page = app.activeDocument.pages[i];
                var textFrames = page.textFrames;
                for (var j = 0; j < textFrames.length; j++) {
                    var textFrame = textFrames[j];
                    var paragraphs = textFrame.paragraphs;
                    for (var k = 0; k < paragraphs.length; k++) {
                        var paragraph = paragraphs[k];
                        var fontSize = paragraph.pointSize;
                        if (fontSize >= 9.5 && fontSize <= 11) {
                            var lastLine = paragraph.lines[-1];
                            if (lastLine && lastLine.words.length === 1) {
                                reportWidow += "PAGE " + (i + 1) + ",     PARAGRAPH " + (k + 1) + "\r";
                                    reportWidow += lastLine.words[0].contents + "\r\r";
                                }
                            }
                        }
                    }
                }
    
            if (reportWidow !== "HERE ARE YOUR RESULTS:\n") {
                text_area.text = reportWidow; // <------------------ HERE
            } else {
                alert("Congratulations, No Widows Were Found!");
            }
        }
    };
    
    // Close Button Click Event
    button2.onClick = function() {
        Widows_Report.close();
    };
    
    Widows_Report.show();
    

    enter image description here