Copy link to clipboard
Copied
Hi,
I've recently started Illustrator scripting for my own purposes, but it is sometimes difficult to transfer InDesign knowledge.
Could anybody please tell me what's wrong with the following trivial script?
#target illustrator
#targetengine "main"
function helloDocument() {
try {
alert(app.activeDocument.name);
} catch( ex ) {
alert(ex);
}
}
var w = new Window("palette","Test",undefined,{ resizeable: true });
var b = w.add("button",undefined,"Here!");
b.onClick = helloDocument;
w.show();
In InDesign the targetengine "main" would be a no-go, because it is the only transient engine - variables are gone after run. Even though the "JavaScript Tools Guide CC" still describes the targetengine directive as supported by Illustrator CS5(!), I did not find a way to create an own targetengine. The "main" engine appears to work persistent in Illustrator - as mentioned in another thread. Even though I'd prefer an own playing ground, I can live with that failure for now.
Anyway, my actual problem is that the active document is not accessible from the button click handler. The palette window shows up and a click invokes the handler, but all I get is an exception "there is no document". When I change the "palette" to "dialog", the otherwise unmodified script works.
Thanks for any clues,
Dirk
Copy link to clipboard
Copied
It seems to work by adding a pair or parentheses for a function:
b.onClick = helloDocument();
Copy link to clipboard
Copied
Assigning the function itself is intended here.
Your suggestion would invoke it once and assign the result.
Copy link to clipboard
Copied
In the meantime I found that if I launch my script via File/Scripts menu the #targetengine directive will do its duties.
I only had tried via startup script, assuming I would need some more unknown code to set up a menu entry, matching InDesign's scriptable menus.
Copy link to clipboard
Copied
Hi Dirk, you need to use BridgeTalk to have your palette talk to Illustrator, see this recent post
Is it possible to use AI "selection sets" in script and scriptUI?