Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Preview in the script

Explorer ,
Jan 19, 2025 Jan 19, 2025

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?

TOPICS
Bug , EPUB , Experiment , Feature request , How to , Import and export , Scripting , SDK , UXP Scripting
139
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 19, 2025 Jan 19, 2025
Translate
Community Expert ,
Jan 19, 2025 Jan 19, 2025

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 19, 2025 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2025 Jan 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. 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 19, 2025 Jan 19, 2025
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines