ScriptUI palette doesn't display when created inside an event listener
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
I think similar issues came up in the past. I don't remember details. What happens if you create the dialog as a global variable?
Harbs
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Thanks both of you for your response. It looks indeed as if the palette needs to be created outside the handler. (w is indeed redeclared by accident. . .).
Thanks,
Peter
Copy link to clipboard
Copied
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)
Copy link to clipboard
Copied
Thanks, Marijan, that works great.
Peter

