Question
• "Error: there is no document" (Adobe Illustrator Javascript)
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
}
};
