Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ScriptUI palette doesn't display when created inside an event listener

Community Expert ,
Apr 18, 2012 Apr 18, 2012

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);

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 18, 2012 Apr 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2012 Apr 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2012 Apr 18, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Apr 18, 2012 Apr 18, 2012

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)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2012 Apr 19, 2012
LATEST

Thanks, Marijan, that works great.

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines