Skip to main content
Inspiring
January 19, 2025
Answered

Preview in the script

  • January 19, 2025
  • 1 reply
  • 304 views

Hello. I have been trying to implement the preview checkbox for quite some time (when it is active, it should show the result that will be displayed). Is it possible to implement this at all? Here is a test script where I tried to do it, but I always get the error ‘Cannot handle the request because a modal dialog or alert is active’.

var dialog = new Window("dialog", "test");
dialog.orientation = "column";
dialog.alignChildren = "left";

var groupSize = dialog.add("group");
groupSize.orientation = "row";
groupSize.alignChildren = "top";

var groupWidth = groupSize.add("group");
groupWidth.orientation = "column";
groupWidth.alignChildren = "left";
groupWidth.add("statictext", undefined, "Width:");
var widthInput = groupWidth.add("edittext", undefined, "10");
widthInput.characters = 10;

var groupHeight = groupSize.add("group");
groupHeight.orientation = "column";
groupHeight.alignChildren = "left";
groupHeight.add("statictext", undefined, "Height:");
var heightInput = groupHeight.add("edittext", undefined, "10");
heightInput.characters = 10;

var previewCheck = dialog.add("checkbox", undefined, "Preview");

var groupButtons = dialog.add("group");
groupButtons.orientation = "row";
var okButton = groupButtons.add("button", undefined, "OK", {name: "ok"});
var cancelButton = groupButtons.add("button", undefined, "Відміна", {name: "cancel"});

var rectangle = null;
function createRectangle() {
    if (rectangle !== null) {
        rectangle.remove();
        rectangle = null;
    }
    try {
        var width = Number(widthInput.text) * 2.834645669;
        var height = Number(heightInput.text) * 2.834645669;
        if (app.documents.length == 0) {
            alert("Error");
            return;
        }
        
        var doc = app.activeDocument;
        var page = doc.pages[0];
        rectangle = page.rectangles.add({
            geometricBounds: [
                page.bounds[0] + (page.bounds[2] - page.bounds[0] - height) / 2,
                page.bounds[1] + (page.bounds[3] - page.bounds[1] - width) / 2,
                page.bounds[0] + (page.bounds[2] - page.bounds[0] + height) / 2,
                page.bounds[1] + (page.bounds[3] - page.bounds[1] + width) / 2
            ],
            strokeWeight: 1,
            strokeColor: doc.swatches.item("Black")
        });
        
    } catch(e) {
        alert("Error: " + e);
    }
}

previewCheck.onClick = function() {
    if (this.value && widthInput.text && heightInput.text) {
        createRectangle();
    } else if (!this.value && rectangle !== null) {
        rectangle.remove();
        rectangle = null;
    }
}

widthInput.onChanging = heightInput.onChanging = function() {
    if (previewCheck.value) {
        createRectangle();
    }
}

if (dialog.show() == 1) { // OK
    if (rectangle === null) {
        createRectangle();
    }
} else { // Cancel
    if (rectangle !== null) {
        rectangle.remove();
    }
}

 What do I need to fix?

Correct answer Robert at ID-Tasker

Unfortunately, I'm not JS guy - what I've posted earlier is based on quick Googling. 

 

I think you also need to add #targetengine and probably a bit more. 

 


https://community.adobe.com/t5/indesign-discussions/scriptui-palette-window/td-p/2045336

 

1 reply

Robert at ID-Tasker
Legend
January 19, 2025

You need to switch to "pallet" instead of "dialog". 

 

FunfyAuthor
Inspiring
January 19, 2025

Excuse me, but can I at least give you a banal example? Because I don't understand how to work with pallet. I have a window that opens and closes itself a second later.

Robert at ID-Tasker
Legend
January 19, 2025

Unfortunately, I'm not JS guy - what I've posted earlier is based on quick Googling. 

 

I think you also need to add #targetengine and probably a bit more.