The script below creates a script menu action, adds an item to InDesign's Window menu, which, when selected, invokes an event listener. This listener in turn creates a ScriptUI palette if necessary and shows it. But the problem is that the palette isn't shown. When I create a dialog instead of a palette it works fine. And if I first create the palette in a separate script, close it, and then run the below script, everything works as expected (the palette is shown). It looks as if I don't understand some interaction between target engines, event listeners, and palettes. Hope anyone can enlighten me.
Thanks,
Peter
#target indesign;
#targetengine "Grrrr";
var utils = app.menus.item("Main").menuElements.item("Window").menuElements.item("Utilities");
var sma = app.scriptMenuActions.add("No break");
sma.addEventListener('onInvoke',
function()
{
var w = Window.find ('palette', 'NoBreak');
if (w == null)
{
var w = new Window ('palette', 'NoBreak')
w.orientation = "row";
w.add ('statictext', undefined, "+/\u2212");
w.add ('statictext', undefined, "No break");
}
w.show();
}
);
app.menus.item("$ID/Main").submenus.item("$ID/&Window").menuItems.add(sma, LocationOptions.after, utils);
Hi Peter,
I think the variable w—which by the way is redeclared in your code—should be declared outside of the anonymous event handler, i.e. at the session scope level. The reason for that is not obvious to me, but I suspect that the garbage collection mechanism automatically destroys the palette as soon as the last reference to it is out of scope (which is the case of w so far). My assumption is the following: since a palette needs a persistent scope to work, the reference to the palette objet needs to be persistent too. (Maybe…)
@+
Marc
Peter,
Just declare w outside the anonymous function:
#target indesign
#targetengine "Grrrr"
var
utils = app.menus.item("Main").menuElements.item("Window").menuElements.item("Utilities"),
sma = app.scriptMenuActions.add("No break"),
w;
sma.addEventListener('onInvoke',
function()
{
w = Window.find ('palette', 'NoBreak');
if (w == null)
{
w = new Window ('palette', 'NoBreak')
w.orientation = "row";
w.add ('statictext', undefined, "+/\u2212");
w.add ('statictext', undefined, "No break");
}
w.show();
}
);
app.menus.item("$ID/Main").submenus.item("$ID/&Window").menuItems.add(sma, LocationOptions.after, utils);
It worked fine for me.
HTH
--
Marijan (tomaxxi)
North America
Europe, Middle East and Africa
Asia Pacific