Skip to main content
Inspiring
September 20, 2024
Question

•  "Error: there is no document" (Adobe Illustrator Javascript)

  • September 20, 2024
  • 2 replies
  • 860 views

Hi -

 

Made a small script (with hep of ChatGPT).

And I always get thie error message "Error: there is no document".

Any idea why?

 

Of course:

  • I have an open document
  • I have a selected place image
  • I re-started aleady Illustrator
  • I tried several debug stuff

 

Here is the script (debug phase)

 

 

#target Illustrator
#targetengine main

// Define palette position
var myPalette_XPOS = 750;
var myPalette_YPOS = 30;

// PALETTE
// =======
var myPalette = new Window('palette', 'DC TOOLS');
    myPalette.orientation = "column";
    myPalette.alignment = "center"; 
    myPalette.alignChildren = ["center","top"];
    myPalette.spacing = 1;
    myPalette.margins = 2;

var button10 = myPalette.add("button", undefined, "IMG Scale");
button10.alignment = ["center","top"];

myPalette.frameLocation = [myPalette_XPOS, myPalette_YPOS];
myPalette.show();

// Button interaction
button10.onClick = function() {
    alert("Button clicked!"); // Ensure the button works
    try {
        // Debug: Check if any document is open
        if (app.documents.length === 0) {
            alert("No document open.");
            return;
        }

        var myDoc_DC = app.activeDocument;
        alert("Active document: " + myDoc_DC.name); // Active document check

        var IMG_selected = myDoc_DC.selection;
        alert("Selection length: " + IMG_selected.length); // Check selection length

        if (IMG_selected.length === 0) {
            alert("No objects selected.");
            return;
        }

        var selectedItem = IMG_selected[0];
        alert("Selected item's typename: " + selectedItem.typename); // Check typename

    } catch (error) {
        alert("Error: " + error.message); // Catch any runtime error
    }
};

 

 

 

This topic has been closed for replies.

2 replies

Charu Rajput
Community Expert
Community Expert
September 20, 2024

Hi @dimitri_cas, As suggested by @m1b  change palette to dialog and also, adding event on click before you show the window, will solve the problem.

 

Try following.

#target Illustrator
#targetengine main

// Define palette position
var myPalette_XPOS = 750;
var myPalette_YPOS = 30;

// PALETTE
// =======
var myPalette = new Window('dialog', 'DC TOOLS');
myPalette.orientation = "column";
myPalette.alignment = "center";
myPalette.alignChildren = ["center", "top"];
myPalette.spacing = 1;
myPalette.margins = 20;

var button10 = myPalette.add("button", undefined, "IMG Scale");
button10.alignment = ["center", "top"];

myPalette.frameLocation = [myPalette_XPOS, myPalette_YPOS];


// Button interaction
button10.onClick = function () {
    alert("Button clicked!"); // Ensure the button works
    try {
        // Debug: Check if any document is open
        if (app.documents.length === 0) {
            alert("No document open.");
            return;
        }

        var myDoc_DC = app.activeDocument;
        alert("Active document: " + myDoc_DC.name); // Active document check

        var IMG_selected = myDoc_DC.selection;
        alert("Selection length: " + IMG_selected.length); // Check selection length

        if (IMG_selected.length === 0) {
            alert("No objects selected.");
            return;
        }

        var selectedItem = IMG_selected[0];
        alert("Selected item's typename: " + selectedItem.typename); // Check typename

    } catch (error) {
        alert("Error: " + error.message); // Catch any runtime error
    }
};

myPalette.show();

 

Best regards
Inspiring
September 20, 2024

Hi charu  🙂

 

Thx, for your reply.

 

Unfortunately, after updating the script by yours, quitting Illustrator and relaunch it,

well Illustrator is stuck at launch after displaying the small "new dialog window" (but I can click on the button).

 

Strange…

 

m1b
Community Expert
Community Expert
September 20, 2024

Hi @dimitri_cas, could you try changing "palette" to "dialog" and see if that helps? Palettes are more involve to get working and will need some extra code.

- Mark

Inspiring
September 20, 2024

Hi Mark -

 

Thanks for your reply.

I did that (in the script), closed and reopen Illustrator. 

Since then Illustrator is stuck at launch 😕😕

 

Really stange…